开发者

ASP.net MVC - Text Box value retains the value that was POSTed

I have a form in my MVC application that has a textbox. When the form is POSTed to the page, I make a modification to the model value and redisplay the view. The textbox still shows the value that was POSTed though.

Here is the code:

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

public class TestController : Controller {
    public ActionResult Index() {
        return View();
    }

    [HttpPost]
    public ActionResult Index(MyModel model) {
        model.Foo += "bar";

        return View(model);
    }
}

Whatever is in the form (lets say I type in foo) I add "bar" and try to show the form again. But when the form 开发者_如何转开发is redisplayed all I see is foo. I remember reading something about why this is happening but can't seem to find it now. I know this is a poor example but I'm just trying to remember why it is doing this, and what the workaround is (other than redirecting). Is there a way to have it show the updated model value and not what the form value that was posted?


You can clear the model state and the helper should display the new value.

[HttpPost]
public ActionResult Index(MyModel model) {
    model.Foo += "bar";
    ModelState.Clear();
    return View(model);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜