6

I have a strange problem with my view in the MVC 3 project. I have a standard view for data editing (created using the template). When I submits the form, I change the Name property, but after I came back to the browser from the controller I still see the lorem value. Why ?

    @using (Html.BeginForm())
    { 
        @Html.EditorFor(model => model.Name)
        <input type="submit" value="Save"  />
    }


    public ViewResult EditUserData(int id)
    {
        [...]
        UserData model = new UserData();
        model.Name = "lorem";            

        return View("~/Views/UserDetails.cshtml", model);
    }

    [HttpPost]
    public ViewResult EditUserData(UserData model)
    {
        model.Name = "ipsum";
        return View("~/Views/UserDetails.cshtml", model);    
    }

public class ControlUserData
{
    [...]

    [Required]
    [Display(ResourceType = typeof(Resources), Name = "UserNameFirst")]
    public string Name { get; set; }
}
Tony
  • 12,405
  • 36
  • 126
  • 226
  • did the post action being hit ? visual studio breakpoints ?? – Shyju Jun 11 '12 at 19:12
  • @Shyju Yes, the debugger invokes the HttpPost's Action after I hit the submit button – Tony Jun 11 '12 at 19:13
  • Why `View("~/Views/UserDetails.cshtml", Model)` instead of putting it in the shared directory and doing `View("UserDetails", Model)`? – Erik Philips Jun 11 '12 at 19:14
  • @Tony : Is the provided code is same as what you have ( for the post action method) ? Do you have ModelState.IsValid property checking statement ?? – Shyju Jun 11 '12 at 19:14
  • @Erik Philips the view's path is modified in my post – Tony Jun 11 '12 at 19:18

4 Answers4

8

You need to remove the value from the ModelState if you want to modify it in a post/get:

[HttpPost]
public ViewResult EditUserData(UserData model)
{
    ModelState.Remove("Name");
    model.Name = "ipsum";
    return View("~/Views/UserDetails.cshtml", model);    
}

This is the built in MVC behavoir: all the Html.Helpers prefers the values in the ModelState collection over the actual model values.

There is a good article about this here: ASP.NET MVC Postbacks and HtmlHelper Controls ignoring Model Changes.

nemesv
  • 138,284
  • 16
  • 416
  • 359
3

This is by design. MVC is assuming that you want to show what the user initially submitted when processing a post action. See this related stack overflow post.

Community
  • 1
  • 1
stephen.vakil
  • 3,492
  • 1
  • 18
  • 23
0

Just a small Edit... for refreshing the entire Model

[HttpPost]
public ViewResult EditUserData(UserData model)
{
   UserData newmodel = new UserData();
   ModelState.Clear();
   model = newmodel;
   return View("~/Views/UserDetails.cshtml", model);    
}
ahaliav fox
  • 2,217
  • 22
  • 21
0

Updating ModelState (on postback the View uses this collection, not the passed model) for a single field has an easier workaround. Here's what I just did to update a specific field before returning to the same view:

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

Depending on the version of .NET/MVC you're using, ModelStateDictionary.SetModelValue() has other, simpler, overloads.

Ryan Russon
  • 1,051
  • 9
  • 16