使用bind编辑表时,只返回一些列错误

时间:2016-11-09 06:40:07

标签: asp.net asp.net-mvc-5

这是模型:

public class Test
    {
        [Key]
        public int Id{ get; set; }
        public string Serial{ get; set; }
        public string Name{ get; set; }
    }

使用帖子进行编辑

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name")] Test test)
    {
        if (ModelState.IsValid)
        {
            db.Entry(test).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(test);
    }

如果您发现编辑帖子上没有串行绑定,那么我的序列空白或空白

1 个答案:

答案 0 :(得分:1)

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name,Serial")] Test test)
    {
        if (ModelState.IsValid)
        {
            db.Tests.Attach(test); //Tests is your controller i guess
            db.Entry(test).Property(x => x.Name).IsModified = true;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(test);
    }