Revert modified viewModel to view

I want to do something like this:

[HttpPost]
public ActionResult Index(Foo foo)
{
    foo.Name = "modified";

    return View(foo);
}

but when my view is displayed, it always has the old meanings! How can I change and return? Do I have to clear ModelState every time?


My opinion:

@model MvcApplication1.Models.Foo


@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Name)
    @Html.TextBoxFor(m => m.Description)

    <input type="submit" value="Send" />
}
+4
source share
2 answers

I think this can be expected because the β€œnormal” scenario when you send the same model back to the view is when the model has errors.

See: http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx

+4
source

MVC ModelState , . , - :

var newVal = new ValueProviderResult("updated value", "updated value", CultureInfo.InvariantCulture);
ModelState.SetModelValue("MyFieldName", newVal);

: ModelStateDictionary.SetModelValue()

0

All Articles