0

模型:

教师班

[Key]
[Required(ErrorMessage="*Enter Faculty id")]
[StringLength(5)]
public string Id { get; set; }

[Required(ErrorMessage="*Enter First Name")]
[MaxLength(30)]
public string F_Name { get; set; }

[Required(ErrorMessage="*Enter Last Name")]
[MaxLength(30)]
public string L_Name { get; set; }

[MaxLength(30)]
public string M_Name { get; set; }

[Required(ErrorMessage="*Enter Email id")]
[MaxLength(50)]
public string Email{ get; set; }

[Required(ErrorMessage="*Enter Department")]
public Int16 Dept_Id { get; set; }
[ForeignKey("Dept_Id")]
public virtual Department Dept { get; set; }

部门类:

public class Department  //static table
{
    [Key]
    public Int16 Id { get; set; }
    [Required]
    [MaxLength(20)]
    public string Dept_Name { get; set; }
}

看法:

<div class="editor-label">
        Faculty Id
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Id, new { style = "width:200px;"})
        @Html.ValidationMessageFor(model => model.Id,null, new { style="color:Red"})
    </div>

     <div class="editor-label">
        First Name
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.F_Name,new { style = "width:200px;"})
        @Html.ValidationMessageFor(model => model.F_Name, null, new { style="color:red;"})
    </div>

    <div class="editor-label">
        Middle Name
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.M_Name,new { style = "width:200px;"})
        @Html.ValidationMessageFor(model => model.M_Name)
    </div>

    <div class="editor-label">
        Last Name
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.L_Name,new { style = "width:200px;"})
        @Html.ValidationMessageFor(model => model.L_Name, null, new { style="color:red;"})
    </div>

    <div class="editor-label">
        Department
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model =>model.Dept_Id ,ViewBag.Dept_Id as IEnumerable<SelectListItem>,
        string.Empty,new { style = "width:200px;text-align:center;"})
        @Html.ValidationMessageFor(model => model.Dept_Id, null, new { style="color:red;"})
    </div>


    <p>
        <input type="submit" value="Create" />
    </p>

控制器:

[HttpPost]
public ActionResult Create(Faculty faculty)
{
    faculty.Email = faculty.F_Name + "." + faculty.L_Name + "@mitcoe.edu.in";
    if (ModelState.IsValid)
    {
        db.Faculty.Add(faculty);
        db.SaveChanges();
        return RedirectToAction("Index");
    }


    ViewBag.Dept_Id = new SelectList(db.Department, "Id", "Dept_Name", faculty.Dept_Id);
    return View(faculty);
}

下拉列表工作正常,所有字段的 ID 都从视图中正确获取。
但是在 post 函数中,modelstate 是无效的,因为它Dept_name是 null。
ORM 仅Dept_Id使用外键创建表。
为什么控制器Dept_Name也在期待?我的模型有什么问题吗?

4

1 回答 1

1

控制器期待Dept_Name,因为:

  • 您是对Faculty类的模型绑定。
  • Department是 的一个组成部分Faculty
  • Dept_Name是 上的必填字段Department

Dept_Name当模型绑定解析表单数据时为 null,因为在您查看的任何地方都没有它作为输入。

两个建议,要么:

  • Have a separate FacultyInputModel class which is the parameter to the Create action method. FacultyInputModel includes only those properties which you expect to be returned from the form;
  • Or: Use HiddenFor with the Dept_Name property, so it is included in your form data posted from you view and the model state is valid.

I'd recommend the first personally. It can be useful sometimes to separate your view models, i.e. what data you are displaying, from your input models, i.e. what data you expect to be posted back. It does add more classes and complexity though, on the down side. See e.g. https://www.simple-talk.com/dotnet/asp.net/the-three-models-of-asp.net-mvc-apps/ for a bit about input models.

于 2013-04-28T10:27:27.890 回答