我有2个模型,我需要在学生创建页面上显示“组”下拉菜单。现在我有页面,我可以选择组,但我在提交后看不到选择的变体(student.Group在[POST]索引中为空)。可能是什么问题?
public class Student
{
[Key]
[ScaffoldColumn(false)]
public string Id { get; set; }
[Display(Name = "Group")]
public virtual Group Group { get; set; }
}
public class Group
{
public string Id { get; set; }
public string Name { get; set; }
}
public class StudentsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Create()
{
ViewBag.MyGroups = db.Groups.ToList();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id, FIO, Group")] Student student)
{
// Here student.Group is null
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
}
Create.cshtml
@model TestSystem.Models.Student
@{
ViewBag.MyGroups = new SelectList(ViewBag.MyGroups, "Id", "Name");
}
...
<div class="form-group">
@Html.LabelFor(model => model.Group, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownListFor(model => model.Group, (SelectList)ViewBag.MyGroups) )
@Html.ValidationMessageFor(model => model.Group, "", new {@class = "text-danger"})
</div>
</div>
答案 0 :(得分:1)
您无法通过HTML表单元素(如选择框Group
)发布@Html.DropDownListFor
等复杂对象。您应该只使用组ID作为视图模型的一部分绑定在表单帖子上,然后使用它来构造或查找控制器中正确的Group
对象。
public class Student
{
[Key]
[ScaffoldColumn(false)]
public string Id { get; set; }
[Display(Name = "Group")]
public int GroupId { get; set; }
public virtual Group Group { get; set; }
}
<div class="form-group">
@Html.LabelFor(model => model.GroupId, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownListFor(model => model.GroupId, (SelectList)ViewBag.MyGroups) )
@Html.ValidationMessageFor(model => model.GroupId, "", new {@class = "text-danger"})
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id, FIO, GroupId")] Student student)
{
// Lookup group via GroupId, or construct a new Group object on `student`
// build student for database and save
...
return View(student);
}
但是,如果它代表您的数据实体,您可能不应该直接使用Student
类作为视图模型。创建一个单独的模型StudentViewModel
,用于实现页面上所需的表单元素。
public class StudentViewModel
{
[Display(Name = "Group")]
public int GroupId { get; set; }
}
<div class="form-group">
@Html.LabelFor(model => model.GroupId, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownListFor(model => model.GroupId, (SelectList)ViewBag.MyGroups) )
@Html.ValidationMessageFor(model => model.GroupId, "", new {@class = "text-danger"})
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "GroupId")] StudentViewModel model)
{
if (ModelState.IsValid)
{
// Lookup group via model.GroupId, or construct a new Group object
// var group = db.Groups.GetByGroupId(model.GroupId);
var student = new Student { Group = group };
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}