MVC 3绑定动态查看数据建模 [英] MVC 3 Binding Dynamic View Data to Model
问题描述
我不知道我是否正确创建这个话题,但基本上我有一些在我看来,文本字段和下拉菜单,他们被安排在这样一种方式,看起来像一排。一些字段包括重量,高度,长度等,并在字段一起组成一个逻辑项。当用户点击一个按钮旁边的排它动态地创建相同的文本框和下拉菜单的新行,从而增加了又一个项目,以形式。我的问题是你如何获得新行绑定到模型的属性?因此,例如这里在模型中的字段:
I'm not sure if I created this topic correctly, but basically I have a number of text fields and drop downs in my view and they are arranged in such a way as to look like a row. Some of the fields include Weight, Height, Length, etc. and the fields together compose a logical item. When the user clicks on a button next to the row it dynamically creates a new row of the same textfields and dropdowns, thus adding another "item" to the form. My question is how do you get the new rows to bind to a property in the model? So for example here are the fields in the model:
[Required]
[Display(Name = "Weight")]
public string Weight { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Class")]
public string Class { get; set; }
[Required]
[Display(Name = "Number of Units")]
public string NumberOfUnits { get; set; }
[Display(Name = "Length")]
public string Length { get; set; }
[Display(Name = "Width")]
public string Width { get; set; }
[Display(Name = "Height")]
public string Height { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Type")]
public string Type { get; set; }
如果用户只使用第一行,这是细,因为它适当的文本框和下拉菜单映射到模型,但添加这些元素的额外的行不会至于模型而言工作。如何获取模型任何想法绑定到动态创建的元素呢?谢谢你。
If the user only uses the first row this is fine since it properly maps the textfields and dropdowns to the model, but adding an additional row of these elements won't work as far as the model is concerned. Any ideas on how to get the model to bind to the dynamically created elements? Thanks.
推荐答案
我会建议你的以下博客帖子。它说明在详细介绍了如何实现这一目标。
I would recommend you the following blog post. It explains in details how to achieve this.
这篇关于MVC 3绑定动态查看数据建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!