Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass multiple models to partial views in ASP.NET MVC

I've been reading Scott Guthrie's post on Passing ViewData from Controllers to Views, but I don't think the lesson is clicking for my specific situation.

(Note: Due to client proprietary restrictions, I can't talk paste the actual code, so I apologize if my made up case is a bit stupid/confusing.)

I have a Controller called ScenarioController which handles the various actions revolving around the creation of a Scenario model. Various actions a user will complete are the general CRUD of Scenarios. I can create a website that does this for the Scenario model. However, I recently updated the Scenario model so that it is now made up of various sub-components (lists of other objects). The corresponding view utilizes jQuery Tabs which load partial views to, ultimately, load the forms to the various sub-component data. Unfortunately, this is where I am having trouble.

My Index page currently looks something like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<% using (Html.BeginForm()) {%>
    <div id="scenario">
        <div id="tabs">
            <ul>
                <li><a href="#tab1">Tab 1</a></li>
                <li><a href="#tab2">Tab 2</a></li>
                <li><a href="#tab3">Tab 3</a></li>
            </ul>

            <div id="tab1"><% Html.RenderPartial("Tab1"); %></div>
            <div id="tab2"><% Html.RenderPartial("Tab2"); %></div>
            <div id="tab3"><% Html.RenderPartial("Tab3"); %></div>
        </div>

        <div class="submitButtons">
            <input type="button" value="Save Scenario" id="SaveScenario" />
            <input type="button" value="Submit Scenario" id="SubmitScenario" />
        </div>
    </div>
<% } %>
</asp:Content>

And the partial pages are strongly-typed to whatever they represent (primarily List<SomeObject>).

How should the data be stored within the Scenario model? I am using a SQL database and interfacing with Entity Framework. Do I still need Properties representing the various lists of items (so I can pass ViewData using strongly typed classes), or is this something I can pass in the ViewData directly from the entity calls (and cast as needed)?

like image 763
JasCav Avatar asked Sep 22 '10 14:09

JasCav


People also ask

How do you pass multiple models to partial view?

Using Render Action Method We can render some part of a view by calling a controller action method using the Html. RenderAction method. The RenderAction method is very useful when we want to display data in the partial view. The disadvantages of this method is that there are only multiple calls of the controller.

Can we bind multiple models to view?

We can Bind Multiple Models with the help of Json as well. We will retun JsonResult from action Method and on View through JQuery, we can parse the JSON data and Bind on View. Here is the code. Change your action method so that Action Method can return the JSON.

Can we use multiple partial view in MVC?

Thank you for answer but there is no multiple partial.


1 Answers

You can pass as many models as you want in your view. you just have to make an encapsulating model which can contain all your 'to send' models.

you can also make use of ViewData but using a model is recommended.

Do your 3 tabs need the whole model or just a part of it?

let's say you have 3 models in your encapsulating model. named tab1 tab2 tab3.

so then you could send each Renderpartial it's appropriate model as so:

        <div id="tab1"><% Html.RenderPartial("Tab1", Model.tab1); %></div>
        <div id="tab2"><% Html.RenderPartial("Tab2", Model.tab2); %></div>
        <div id="tab3"><% Html.RenderPartial("Tab3", Model.tab3); %></div>

your encapsulating model could then look like

namespace MVCNAMESPACE.Models {
    public partial class EnCapModel {
        public List<SomeObject> tab1 { get; set; }
        public List<SomeObject> tab2 { get; set; }
        public List<SomeObject> tab3 { get; set; }
    }
}

hope this helps

like image 102
Stefanvds Avatar answered Oct 25 '22 19:10

Stefanvds