繁体   English   中英

使用 ASP.NET MVC 2 传递 Model 子 class 的多个实例

[英]Passing Multiple Instances of a Model sub class, Using ASP.NET MVC 2

I'm trying to pass multiple instances of a sub-class of a model in my MVC web application, but I'm not sure how to go about the approach or if my idea would even be applicable in a MVC web application.

这是我正在考虑使用的 Model 的示例:

class MainSampleModel {
    public datatype Prop1;
    public datatype Prop2;
    public List<SubSampleClass> Prop3;
}

class SubSampleModel {
    public datatype SubProp1;
    public datatype SubProp2;
}

我将需要传递至少五个 SubSampleModel 实例,以便为 MainSampleModel 中的 Prop3 值添加。 我不确定如何使视图适用于此操作过程。

您的视图可以采用强类型 model (在本例中为MainSampleModel ),因此视图类似于:

<%@ Page Language="C#" MasterPageFile="Your.Master" Inherits="System.Web.Mvc.ViewPage<MainSampleModel>" %>

<% foreach (var subModel in Model.Prop3) { %>
    <div><%:subModel.SubProp1 %></div>
<% } %>

当然,您可以将 foreach 循环中的内容转移到部分视图中,但是上面的内容可以让您处理集合中的每个项目。

这是你追求的那种东西吗? 如果没有,如果您能进一步澄清,我会尽力提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM