如何在 ASP.NET MVC 中将多个模型传递给部分视图

发布于 2024-09-24 15:30:14 字数 1895 浏览 1 评论 0原文

我一直在阅读 Scott Guthrie 的文章 将 ViewData 从控制器传递到视图,但我认为该课程不适合我的具体情况。

(注意:由于客户端专有限制,我无法谈论粘贴实际代码,所以如果我编造的情况有点愚蠢/令人困惑,我深表歉意。)

我有一个名为 ScenarioController 的控制器,它处理围绕创建的各种操作场景模型的一部分。用户将完成的各种操作是场景的通用CRUD。我可以创建一个网站来为场景模型执行此操作。但是,我最近更新了场景模型,使其现在由各种子组件(其他对象的列表)组成。相应的视图利用 jQuery Tabs 加载部分视图,最终将表单加载到各个子组件数据中。不幸的是,这就是我遇到麻烦的地方。

我的索引页面当前看起来像这样:

<%@ 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>

部分页面对于它们所代表的任何内容都是强类型的(主要是 List)。

数据应该如何存储在场景模型中?我正在使用 SQL 数据库并与实体框架连接。我是否仍然需要表示各种项目列表的属性(以便我可以使用强类型类传递 ViewData),或者这是我可以直接从实体调用传递 ViewData 的东西(并根据需要进行转换)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-10-01 15:30:14

您可以在视图中传递任意数量的模型。您只需制作一个封装模型,其中可以包含所有“要发送”的模型。

您还可以使用ViewData,但建议使用模型。

您的 3 个选项卡需要整个模型还是只需要其中的一部分?

假设您的封装模型中有 3 个模型。命名为 tab1 tab2 tab3。

那么您可以发送每个 Renderpartial 它是适当的模型,如下所示:

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

您的封装模型可能看起来像

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; }
    }
}

希望这会有所帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文