Asp.net mvc 2 理解MVC中的表单:如何从列表中填充模型<&燃气轮机;

Asp.net mvc 2 理解MVC中的表单:如何从列表中填充模型<&燃气轮机;,asp.net-mvc-2,viewmodel,Asp.net Mvc 2,Viewmodel,结论在图像中,可在底部找到 我很难理解MVC中的表单是如何工作的(因为我是一名WebForms开发人员,非常想在一个大项目中开始使用MVC) 我接受了MVC2 Web项目,并向其中添加了一个简单的ViewModel namespace TestForms.Models { public class myFormViewModel { public myFormViewModel() { this.Options = new List<myList>(

结论在图像中,可在底部找到


我很难理解MVC中的表单是如何工作的(因为我是一名WebForms开发人员,非常想在一个大项目中开始使用MVC)

我接受了MVC2 Web项目,并向其中添加了一个简单的ViewModel

namespace TestForms.Models
{
    public class myFormViewModel
    {
        public myFormViewModel() { this.Options = new List<myList>(); }

        public string Name { get; set; }
        public string Email { get; set; }

        public List<myList> Options { get; set; }
    }

    public class myList
    {
        public myList() { this.Value = this.Name = ""; this.Required = false; }

        public string Name { get; set; }          
        public string Value { get; set; }      
        public string Type { get; set; }
        public bool Required { get; set; }
    }
}
model
是传递给视图的模型变量的名称
Options
是类型为
List
然后我们使用属性名


查看您的UI,您似乎没有将来自选项成员的数据放在其上

<% foreach (myList obj in Model.Options) { %>
     // Add the object to your UI. they will be serialized when the form is submitted
<% } %>

//将对象添加到UI中。在提交表单时,它们将被序列化
还要检查是否将数据包含在表单元素中

编辑:


对不起!我没有意识到你在控制器里面填充对象。您能在视图中显示您的代码吗?

我显示了,我显示了,否则它将永远不会显示
年龄
ZipCode
,因为它属于传递到索引
视图的对象。。。添加了伊藤忠的图片!看看我不久前在这里提出的这个问题。它将为您提供使默认模型绑定器序列化复杂对象所需的所有步骤<代码>http://stackoverflow.com/questions/3800305/building-a-complex-object-step-by-step-where-to-save-it
我正在尝试,但我不明白,在您的示例中,您不使用强类型视图,而是使用ViewData/Session跟踪值。我尝试使用
,但我得到了相同的结果,我的
选项没有以任何方式填充:(@balexandre:关键在于为每个属性使用完全限定名。如果您在某个点上查看答案,它会说:“每个字段也应该具有ModelName.property的id,以便控制器知道属性在哪里…”后面是一个代码示例…如果您使用完全限定的名称命名属性,模型绑定器将知道将该属性放置在何处…谢谢,这正是我缺少的:)顺序部分。非常感谢你给我带来的麻烦,给我解释一下!糟糕的是,我不能给+5
<% foreach (myList obj in Model.Options) { %>
     // Add the object to your UI. they will be serialized when the form is submitted
<% } %>