簡體   English   中英

如何在ASP.NET MVC4中正確進行編輯操作?

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

我正在嘗試以這種方式進行更新:

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

使用此更新OS行后,該行將不會更新。 請問這是怎么了?

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(){}
}

知道在調試模式下,我將獲得設置為0的idOS之外的輸入os的完整數據。

問題是您必須在_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