简体   繁体   English

如何在ASP.NET MVC4中正确进行编辑操作?

[英]How to make an Edit operation properly in ASP.NET MVC4?

I'm trying to make an update this way : 我正在尝试以这种方式进行更新:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Modifier(OSModel os)
        {    
            if (ModelState.IsValid)
            {
                _db.Entry(os).State = EntityState.Modified;

                // success !
                string str = "o";
                return RedirectToAction("Index", new { str = str });
            }

            // fail !
            return View(os);
        }

After updating an OS row using this, the row does not get updated. 使用此更新OS行后,该行将不会更新。 What's wrong with this please? 请问这是怎么了?

OSModel OS模型

[Table("OS")]
public class OSModel
{
    [Key]
    public int idOS { get; set; }

    [Required]
    public string nameOS { get; set; }

    [Required]
    public string versionOS { get; set; }

    [Required]
    public string editionOS { get; set; }

    [Required]
    public string servicepackOS { get; set; }

    [Required]
    public int bitsOS { get; set; }

    public OSModel(){}
}

Knowing that at the debug mode, I get the full data of the input os aside of idOS which is set to 0. 知道在调试模式下,我将获得设置为0的idOS之外的输入os的完整数据。

Thing is you must add _db.SaveChanges() after _dn.Entry(os).State=... , to save the change in the database Try this: 问题是您必须在_dn.Entry(os).State=...之后添加_db.SaveChanges() ,以将更改保存到数据库中尝试以下操作:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Modifier(OSModel os)
    {    
        if (ModelState.IsValid)
        {

            _db.Entry(os).State = EntityState.Modified;
            _db.SaveChanges();// you must add this line
            // success !
            string str = "o";
            return RedirectToAction("Index", new { str = str });
        }

        // fail !
        return View(os);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM