Note that there are some explanatory texts on larger screens.

plurals
  1. POIn ASP.NET MVC3, how should one render multiple PartialViews backed by multiple Models?
    primarykey
    data
    text
    <p>In MVC3 Razor, how do you create a page with multiple forms, so that each form is a partial view rendered with its own model? </p> <p>We have been trying various forms of calling Html.RenderPartial(), passing in the partialview name as well as an instance of our models that we're accessing through the ViewBag, but every method we've tried seems to have serious issues, and so we must have a fundamental misunderstanding of how this is supposed to operate in an ideal world.</p> <p>We have found some SO responses on similar topics (<a href="https://stackoverflow.com/questions/4419889/asp-net-mvc2-how-to-render-a-view-if-has-multiple-different-models">like this one</a>) recommending a "super model" that contains references to each of the sub models our partial views might need to use, but we cannot find an example of how you would actually use those submodels' data, e.g. when trying to write EditorTemplates.</p> <p><strong>Edit:</strong> Let me clarify my question a bit. We ARE able to render a partialview or even an action (using RenderAction as Mark S. recommended). However, our primary trouble is that when those forms submit back to, e.g. save the state of the Model in that partialview area, the Controller doesn't seem to know what model we're referencing. How should we be calling into the Controller? </p> <p>We've tried a few different ways of rendering these partial views. Our first try was like this:</p> <pre><code>@using (Html.BeginForm("SaveAccountInfo", null, FormMethod.Post, new { id = "section1-form" })) { @Html.EditorForModel("AccountInfo", (OurModels.AccountInfo) ViewBag.account_info) } </code></pre> <p>The ViewBag.account_info contains the Model data for this one partial view. </p> <p>We've also tried:</p> <pre><code>@{Html.RenderPartial("FormAccountInfo", (OurModels.AccountInfo) ViewBag.account_info);} </code></pre> <p>And now:</p> <pre><code>@{Html.RenderAction("FormAccountInfo", "Main", new { account_info = (OurModels.AccountInfo) ViewBag.account_info }); } </code></pre> <p>But no matter which method we use for rendering this one form on the page, when that form gets submitted for processing, we can't seem to access any of the submitted data via the Model? I feel like we're doing something really dumb, but we're new to MVC.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload