[英]pass list of objects from view to controller in asp.net
I am adding checkboxes to every object in a List. 我将复选框添加到列表中的每个对象。 When I try to return the values back to the controller the list is empty and I only get the checkbox bool.
当我尝试将值返回给控制器时,该列表为空,并且仅得到复选框bool。
Can someone explain to me how to pass the List correctly from the view to the controller. 有人可以向我解释如何将List从视图正确传递给控制器。
I tried it with form but I am not sure if this is the correct way. 我尝试过使用表单,但是我不确定这是否正确。
I searched a lot on Google and also found similar posts on stackoverflow but I couldn't find one that helped me. 我在Google上进行了很多搜索,还发现了关于stackoverflow的类似帖子,但找不到帮助我的帖子。
View 视图
@model List<WCFasp.net.WCF.Person>
@{
ViewBag.Title = "ShowView";
}
@using (Html.BeginForm("Check", "Home"))
{
for (int i = 0; i < Model.Count(); i++)
{
<p>@Html.CheckBoxFor(m => m[i].IsChecked) @Html.DisplayFor(m => m[i].Name)</p>
}
<input id="submit" type="submit" value="submit" />
}
Controller 调节器
[HttpPost]
public ActionResult Check(List<WCF.Person> selectedpersonlist)
{
//Here I get the empty list
return View("ShowSelectedView");
}
Person 人
[DataContract]
public class Project
{
[DataMember]
public string Name { get; set; }
[DataMember]
public bool IsChecked { get; set; }
public Project(string name, bool isChecked)
{
this.Name = name;
this.IsChecked = isChecked;
}
}
Little question at the end. 最后一点问题。 Am I getting down voted because I am not a pro or is there another reason?
我是不是专业人士,还是因为其他原因而被否决?
If you want more properties populated within your list then you can use hidden fields to store the information: 如果要在列表中填充更多属性,则可以使用隐藏字段来存储信息:
for (int i = 0; i < Model.Count(); i++)
{
@Html.HiddenFor(m => m[i].Name)
<p>@Html.CheckBoxFor(m => m[i].IsChecked) @Html.DisplayFor(m => m[i].Name)</p>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.