修改后回来不工作字段值 [英] modify field value in post back not working

查看:133
本文介绍了修改后回来不工作字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有asp.net mvc的控制器code的片断是检查状态是无效的,那么我会更新一个字段的值:

I have the following snippet of asp.net mvc controller code that check if state is invalid, then I will update the value of one field:

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

经过调试我检查名称字段的新值被成功地传递到我的视图模型,但在渲染结果,只有现场验证字段更新,字段值变化不会呈现,可能有人帮我如何应用这种变化?

Through debugging I checked that the new value of Name field is successfully passed to Model of my View, but in the render result, only the field validation fields are updated, the field value change is not rendered, Could someone help me on how to apply this change?

推荐答案

您感到有点儿缓存的问题,有明确的:

You got sort of cache problem, clear it with:

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

这篇关于修改后回来不工作字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆