簡體   English   中英

僅使用asp.net mvc4,modelstate.isvalid在編輯視圖中返回false

[英]modelstate.isvalid return false in edit view only using asp.net mvc4

我是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?

這是錯誤快照 在此處輸入圖片說明

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

更改:

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; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM