TextBoxFor not updated after postback

I’m probably wrong. I would appreciate your help in the following. I have an example MVC3 application with a single editable field that is displayed to the user using the 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; }
}

View:

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

Controller:

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

If I replace TextBoxFor with TextBox or DisplayTextFor, then it will work correctly.

+4
source share
1 answer

I suggest that you should call ModelState.Clear()inside your action [HttpPost]before setting a new value.

, : @Html.TextBoxFor(m = > m.MvcGridModel.Rows [j].Id )

: ASP.NET MVC 3 Ajax.BeginForm Html.TextBoxFor , , Ajax.BeginForm, .

, @Scheien:

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

All Articles