本文介绍了Mvc绑定对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我只想绑定一个对象列表,显示它并将其发回,然后在运行时向Add和Delete添加功能。到目前为止,我的问题是我无法在帖子上检索列表。这是我的代码: 模型I just want to bind a list of objects, display it, and post it back before adding the functionality to Add and Delete at run-time. My problem so far is that I'm not able to retrieve the list on post. Here's my code:Models[Serializable]public class Animal{ public int ID { get; set; } public string Name { get; set; }}[Serializable]public class Animals{ public string FarmName { get; set; } public List Farm { get; set; }} 控制器: Controller:public class HomeController : Controller{ public ActionResult Index() { var animals = new Animals(); animals.FarmName = "My Small Farm"; animals.Farm = new List(); animals.Farm.Add(new Animal() { ID = 1, Name = "Dog" }); animals.Farm.Add(new Animal() { ID = 2, Name = "Cat" }); return View(animals); } [HttpPost] public ActionResult Index(Animals farm) { return View(farm); }} 查看: View:@using MVCTest.Models@model MVCTest.Models.Animals@{ ViewBag.Title = "Home Page";}@using (Html.BeginForm("Index", "Home")){ @Html.LabelFor(x => x.FarmName) @Html.TextBoxFor(x => x.FarmName) for (var i = 0; i < Model.Farm.Count; i++ ) { @Html.LabelFor(x => x.Farm[i].ID) @Html.TextBoxFor(x => x.Farm[i].ID) @Html.LabelFor(x => x.Farm[i].Name) @Html.TextBoxFor(x => x.Farm[i].Name) } <input type="submit" value="Submit" />} 我的尝试: 我尝试在视图中使用foreach和for循环,但是他们都没有回发初始条目。此外,我尝试在for循环中添加一个隐藏字段并将其命名为index并将值设置为变量i。但是,这对我来说也不起作用。 我也试过更改List< animals>在模型中,动物为IEnumerable< animals>在控制器中,我制作了一个新的POST方法public ActionResult Index(动物动物,IEnumerable< animal> farm)。但是,对于变量farm,我仍然为null。 我也尝试将LabelFor(x => x.Farm [i] .ID)更改为HiddenFor(x => x.Farm [i]。但是,生成HTML如下所示:< input data-val =truedata-val-number =字段ID必须是数字。 data-val-required =ID字段是必需的。 id =Farm_0__IDname =Farm [0] .IDtype =hiddenvalue =1> 我知道我必须遗漏一些东西然而,在尝试了整整两天后,我感到很茫然。我将非常感谢任何帮助和/或对我所犯错误的指示。What I have tried:I tried using the foreach and the for loops in the View, however both of them didn't post back the initial entries. Also, I tried adding a hidden field in the for loop and named it index and set the value to the variable "i". However, this didn't work for me either. I also tried changing List<animals> in the model Animals to IEnumerable<animals> and in the controller, I made a new POST method "public ActionResult Index(Animals animals, IEnumerable<animal> farm)". However, I still got a null for the variable "farm". I also tried changing the LabelFor(x => x.Farm[i].ID) to HiddenFor(x => x.Farm[i].ID) however, the generate HTML looks like this: <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="Farm_0__ID" name="Farm[0].ID" type="hidden" value="1">I know I must be missing something, however, I'm at a loss after trying for 2 whole days. Any help and/or pointers to what I'm doing wrong would be greatly appreciated.推荐答案 @using (Html.BeginForm("Index", "Home")){ @Html.LabelFor(farm => farm.FarmName) @Html.TextBox("farm.FarmName", Model.FarmName) for (var i = 0; i < Model.Farm.Count; i++) { @Html.LabelFor(x => x.Farm[i].ID) @Html.TextBox(string.Format("farm.Farm[{0}].ID", i), Model.Farm[i].ID) @Html.LabelFor(x => x.Farm[i].Name) @Html.TextBox(string.Format("farm.Farm[{0}].Name", i), Model.Farm[i].Name) } <input type="submit" value="Submit" />} 您还可以查看自定义绑定 http://www.prideparrot.com/blog/archive/2012/6/customizing_property_binding_through_attributes [ ^ ] 这可能有比上述更好的方法,所以看看你是否得到其他答案。You can look at custom binding alsohttp://www.prideparrot.com/blog/archive/2012/6/customizing_property_binding_through_attributes[^]There might be a better way of doing this than the above, so see if you get other answers. public JsonResult OppDetails(int Id) { var data = _crmNewCustomerService.GetOppById(Id).Data; var selected = data.ToList(); List<OpportunityViewModel> opportunityData = new List<OpportunityViewModel>(); foreach (var item in selected) { if (item.OpportunityTitle != null) { OpportunityViewModel model = new OpportunityViewModel(); model.CustomerId = Convert.ToInt32(item.CustomerId); model.OpportunityTitle = item.OpportunityTitle; opportunityData.Add(model); } else { } } return Json(opportunityData , JsonRequestBehavior.AllowGet); } 这篇关于Mvc绑定对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 23:43