大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 如何使用ASP.NET MVC 3编辑IEnumerable?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public class SomeValue { public int id { get; set; } public int Value { get; set; } } public class SomeModel { public String SomeProp1 { get; set; } public String SomeProp2 { get; set; } public IEnumerable<SomeValue> MyData { get; set; } }
我想为SomeModel类型创建一个编辑表单,它将包含SomeProp1和SomeProp2的通常文本字段,然后包含SomeModel.MyData集合中每个SomeValue的文本字段的表。
这怎么做?这些价值观如何回归到模型?
我目前有一个表单显示每个值的文本字段,但它们都具有相同的名称和相同的Id。这显然是无效的HTML,并将阻止MVC将值映射回来。
控制器:
public class HomeController : Controller { public ActionResult Index() { // In the GET action populate your model somehow // and render the form so that the user can edit it var model = new SomeModel { SomeProp1 = "prop1",SomeProp2 = "prop1",MyData = new[] { new SomeValue { Id = 1,Value = 123 },new SomeValue { Id = 2,Value = 456 },} }; return View(model); } [httpPost] public ActionResult Index(SomeModel model) { // Here the model will be properly bound // with the values that the user modified // in the form so you could perform some action return View(model); } }
查看(〜/ Views / Home / Index.aspX):
<% using (Html.beginForm()) { %> Prop1: <%= Html.TextBoxFor(x => x.SomeProp1) %><br/> Prop2: <%= Html.TextBoxFor(x => x.SomeProp2) %><br/> <%= Html.EditorFor(x => x.MyData) %><br/> <input type="submit" value="OK" /> <% } %>
最后编辑器模板(〜/ Views / Home / EditorTemplates / SomeValue.ascX)将自动调用MyData集合的每个元素:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.SomeValue>" %> <div> <%= Html.TextBoxFor(x => x.Id) %> <%= Html.TextBoxFor(x => x.value) %> </div>
以上是大佬教程为你收集整理的asp.net-mvc – 如何使用ASP.NET MVC 3编辑IEnumerable?全部内容,希望文章能够帮你解决asp.net-mvc – 如何使用ASP.NET MVC 3编辑IEnumerable?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。