ASP.MVC 5 创建页面上的下拉菜单

ASP.MVC 5 Dropdown on Create page

我有 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>

您不能通过 HTML 表单元素 select 框 (@Html.DropDownListFor) 来 post 复杂对象 Group。您应该只使用组 ID 作为绑定到表单 post 的视图模型的一部分,然后使用它在控制器中构造或查找正确的 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 class 代表您的数据实体,您可能不应该直接将其用作视图模型。创建一个单独的模型 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);
}