简体   繁体   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. 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.

Here a sample for the Model I'm thinking of using:这是我正在考虑使用的 Model 的示例:

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

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

I'm going to need to pass at least five instances of SubSampleModel to be added for the value of Prop3 in MainSampleModel.我将需要传递至少五个 SubSampleModel 实例,以便为 MainSampleModel 中的 Prop3 值添加。 I'm not sure how to make the View work for this course of action.我不确定如何使视图适用于此操作过程。

your view can take in a strongly typed model (in this case, MainSampleModel ) so a view something like:您的视图可以采用强类型 model (在本例中为MainSampleModel ),因此视图类似于:

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

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

of course, you could farm off the stuff inside the foreach loop to a partial view, but the above will let you work with each item in the collection.当然,您可以将 foreach 循环中的内容转移到部分视图中,但是上面的内容可以让您处理集合中的每个项目。

Is this the sort of thing you were after?这是你追求的那种东西吗? If not, if you can clarify further, and I'll do my best to help.如果没有,如果您能进一步澄清,我会尽力提供帮助。

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

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