modelstate.isvalid仅在编辑视图中使用asp.net mvc4返回false

时间:2016-01-02 23:23:48

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

我是asp.net mvc的新手,我想知道为什么ModelState.IsValid = false ?? 在使用

时的编辑视图中
 if (ModelState.IsValid)
                {
                  //code
                }

此处列出的完整代码: 模型类:

  public class Departments
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Employee> Employeesss { get; set; }


public class Employee
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public int DepartmentId { get; set; }

        [ForeignKey("DepartmentId")]
        public virtual Departments Id { get; set; }
    }


public class dropdownDbContext : DbContext
    {
        public DbSet<Departments> Departments { get; set; }

        public DbSet<Employee> Employees { get; set; }

    }

在控制器中这些是编辑控制器

 public ActionResult Edit(int id = 0)
        {
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
            return View(employee);
        }

        //
        // POST: /empttttttttttttttttt/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));

                // Breakpoint, Log or examine the list with Exceptions.
            }

            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
            return View(employee);
        }

在视图中

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Employee</legend>

        @Html.HiddenFor(model => model.EmployeeId)

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.City)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.City)
            @Html.ValidationMessageFor(model => model.City)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DepartmentId, "Id")
        </div>
        <div class="editor-field">
            @Html.DropDownList("DepartmentId", String.Empty)
            @Html.ValidationMessageFor(model => model.DepartmentId)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

它没有显示错误,但使用

时编辑在此逻辑中无法正常工作
  if (ModelState.IsValid)
                {
                    db.Entry(employee).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

我想知道为什么ModelState.IsValid = false ??

这是错误快照 enter image description here

The parameter conversion from type 'System.String' to type 'dropdown.Models.Departments' failed because no type converter can convert between these types.

1 个答案:

答案 0 :(得分:3)

变化:

if (!ModelState.IsValid)
{
     var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
    // Breakpoint, Log or examine the list with Exceptions.
}

要:

if (!ModelState.IsValid)
{
    var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();
}

然后你把断点放在错误上

编辑:

像这样更改你的模型:

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string City { get; set; }

    [ForeignKey("Departments")]
    public int DepartmentId { get; set; }
    public virtual Departments department { get; set; }
}