Search code examples
asp.net-mvcasp.net-mvc-3asp.net-mvc-viewmodel

How do I populate HTML content with new values once data has been changed on postback?


I have MVC3 razor application. Once I'm submitting a form and in Action i'm changing ViewModel content, i can't see new values populated.

There was a topic about that in MVC2 where guys told that it may be fixed in MVC3 http://aspnet.codeplex.com/workitem/5089?ProjectName=aspnet

Can you tell if there is an option to do that or what is the better way(workaround) to update UI without JavaScript using postbacks?

Action:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    model.Value = "new value"
    return View("Index", model);
}

UI:

@Html.HiddenFor(x => x.Value)

ViewModel:

public class MyViewModel
{
   public string Value { get;set; }
}

Solution

  • Looks like it's using the ModelState values that were posted.

    If you clear the ModelState using ModelState.Clear() the new value you set should be in the hidden field.