开发者

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)?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜