绑定到下拉列表时,ModelState.IsValid为false

时间:2013-04-28 07:13:40

标签: c# asp.net-mvc-4

型号:

教师班

[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?我的模特有什么不对吗?

1 个答案:

答案 0 :(得分:1)

控制器期待Dept_Name因为:

  • 您是Faculty类的模型绑定。
  • DepartmentFaculty
  • 的一个组成部分
  • Dept_NameDepartment上的必填字段。
当模型绑定解析表单数据时,

Dept_Name为null, 因为你在视图中的任何地方都没有它作为输入。

两个建议:

  • 有一个单独的FacultyInputModel课程 Create操作方法的参数。 FacultyInputModel仅包含您希望从表单中返回的属性;
  • 或:将HiddenForDept_Name属性一起使用,以便它包含在从您的视图发布的表单数据中,并且模型状态有效。

我个人推荐第一个。有时可以将您的视图模型(即您正在显示的数据)与输入模型分开,即您希望回发哪些数据。尽管如此,它确实增加了更多的类和复杂性。参见例如https://www.simple-talk.com/dotnet/asp.net/the-three-models-of-asp.net-mvc-apps/了解输入模型。