Asp.net mvc MVC BeginCollectionItem帮助程序未绑定到视图模型中的模型实例
在MVC5中构建动态列表。表单将发布到控制器,我可以看到在chrome开发工具中正确填充的StudentList,但在控制器中,StudentList为空 型号:Asp.net mvc MVC BeginCollectionItem帮助程序未绑定到视图模型中的模型实例,asp.net-mvc,begincollectionitem,Asp.net Mvc,Begincollectionitem,在MVC5中构建动态列表。表单将发布到控制器,我可以看到在chrome开发工具中正确填充的StudentList,但在控制器中,StudentList为空 型号: public class Student { public int id { get; set; } public string Name { get; set; } } public class StudentViewModel { public Student student { get; set; }
public class Student
{
public int id { get; set; }
public string Name { get; set; }
}
public class StudentViewModel
{
public Student student { get; set; }
public List<Student> StudentList { get; set; }
}
到
主计长职位:
[HttpPost]
public ActionResult Create(StudentViewModel model)
当我在帖子到达控制器之前拦截帖子时,我可以看到它已正确绑定,但控制器中的StudentList为空
我试图避免在视图模型中复制学生模型,因为它是重复的,并且所有的验证规则都已在学生模型中,因此我也必须在视图模型中复制所有这些规则,这似乎是错误的。您的
StudentViewModel
很奇怪,为什么在同一视图模型中有Student
和StudentList
?我也没有看到你在表单的任何地方使用StudentList
,你希望它被填充吗?@hoomannbahreini-Student是一个学生,StudentList是学生列表,它是集合在部分视图中绑定的。你的StudentViewModel
很奇怪,为什么在同一视图模型中有Student
和StudentList
?我也没有看到你在表单中的任何地方使用StudentList
,你希望它被填充吗?@HoomanBahreini-Student是一个学生,StudentList是一个学生列表,它是集合在部分视图中绑定的对象。
@model TaskTracker.ViewModels.StudentViewModel
@using (Html.BeginCollectionItem("StudentList"))
{
<div class="form-row">
<div class="form-group col-2">
@Html.EditorFor(model => model.Student.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Student.Name, "", new { @class = "text-danger" })
</div>
</div>
}
@Html.EditorFor(model => model.Student.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
[HttpPost]
public ActionResult Create(StudentViewModel model)