在post post中修改字段值不起作用

cap*_*UBB 1 asp.net-mvc asp.net-mvc-4

我有以下片段的asp.net mvc控制器代码,检查状态是否无效,然后我将更新一个字段的值:

[HttpPost]
public ActionResult Create(ContactInfo contactinfo)
{
    if (IsModelStateValid(GetIssues(contactinfo)))
    {
        db.ContactInfoes.Add(contactinfo);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

   contactinfo.Name+="why this is not working".
    return View(contactinfo);
}
Run Code Online (Sandbox Code Playgroud)

通过调试我检查了Name字段的新值是否成功传递给了我的View模型,但是在渲染结果中,只更新了字段验证字段,没有渲染字段值更改,有人可以帮助我如何申请这个改变?

gdo*_*ica 8

你遇到了一些cache问题,用以下方法清除它:

[HttpPost]
public ActionResult Create(ContactInfo contactinfo)
{
    if (IsModelStateValid(GetIssues(contactinfo)))
    {
        db.ContactInfoes.Add(contactinfo);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    // Clear the model state.
    ModelState.Clear(); // <-----------------------------------------------

    // Or just remove the `Name` property:        
    ModelState.Remove("Name")

    contactinfo.Name+="why this is not working".
    return View(contactinfo);
}
Run Code Online (Sandbox Code Playgroud)