0

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; }
}
Sergejs
  • 2,540
  • 6
  • 32
  • 51

2 Answers2

1

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.

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

You should use form and to post it to action.

@model MyViewModel

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.HiddenFor(x=>x.Value)
    <input type="submit" value="Submit" />
}

Controller

//
public ActionResult Index()
{
    MyViewModel model = new MyViewModel();
    model.Value = "old value";

    return View("Index", model);
}

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    //get posted model values (changed value by view "new value")
    string changed_value = model.Value;

    // you can return model again if your model.State is false or after update
    return View("Index", model);
}
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197