回发后TextBoxFor不刷新 [英] TextBoxFor is not refreshing after postback

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

问题描述

我可能在某个地方犯了一个愚蠢的错误.感谢您在以下方面的帮助.
我有带有单个可编辑字段的示例MVC3应用程序,该字段通过TextBoxFor方法显示给用户.在Index(POST)操作中,我更改了值,但它仍然保持不变.我究竟做错了什么?

I'm probably making a stupid mistake somewhere. I would appreciate your help in the following.
I have sample MVC3 application with a single editable field that is displayed to user with TextBoxFor method. In the Index(POST) action I change the value but it still remains the same. What am I doing wrong?

我的代码:
型号:

My code:
Model:

public class TestModel
{
    public string Name { get; set; }
}

查看:

using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Name)
    <input type="submit" />
}

控制器:

public ActionResult Index()
{
    return View("Index", new TestModel() { Name = "Before post" });
}

[HttpPost]
public ActionResult Index(TestModel model)
{
    model.Name = "After post";
    return View("Index", model);
}

如果我将TextBoxFor替换为TextBox或DisplayTextFor,则它将正常工作.

If I replace TextBoxFor with TextBox or DisplayTextFor then it works correctly.

推荐答案

我相信您必须先在 [HttpPost] 操作中调用 ModelState.Clear()设置新值.

I believe you must call ModelState.Clear() inside your [HttpPost] action, before you set the new value.

根据此答案,其中有一个很好的解释:

According to this answer, which has a very good explanation: How to update the textbox value @Html.TextBoxFor(m => m.MvcGridModel.Rows[j].Id)

也请参见以下内容:

See this too: ASP.NET MVC 3 Ajax.BeginForm and Html.TextBoxFor does not reflect changes done on the server Although it seems you're not using Ajax.BeginForm, the behavior is the same.

包括@Scheien建议的示例:

Including an example as suggested by @Scheien:

[HttpPost]
public ActionResult Index(TestModel model)
{
    ModelState.Clear(); 
    model.Name = "After post";
    return View("Index", model);
}

这篇关于回发后TextBoxFor不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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