3

is it allowed in ASP.NET MVC to alter the submitted values?

[HttpPost]
public ActionResult Create(Person toCreate)
{
    toCreate.Lastname = toCreate.Lastname + "-A-";

    return View(toCreate);
}

i tried that code, but ASP.NET MVC keep showing the values submitted by the user

[UPDATE]

this:

[HttpPost]
public ActionResult Create(Person toCreate)
{
    return View(new Person { Lastname = "Lennon" });
}

or this:

[HttpPost]
public ActionResult Create(Person toCreate)
{
    return View();
}

still shows the values inputted by the user, which led me to thinking, why the generated code need to emit: return View(toCreate) in HttpPost? why not just return View()? at least it doesn't violate the expectations that the values can be overridden from controller

[UPDATE: 2010-06-29]

Found the answer here: ASP.NET MVC : Changing model's properties on postback and here: Setting ModelState values in custom model binder

Working Code:

[HttpPost]
public ActionResult Create(Person toCreate)
{
    ModelState.Remove("Lastname");
    toCreate.Lastname = toCreate.Lastname + "-A-";
    return View(toCreate);
}
Community
  • 1
  • 1
Hao
  • 8,047
  • 18
  • 63
  • 92

3 Answers3

4

Apparently there is no way to revalidate the ModelState once you change a value of some key. The IsValid remains false because setting a new value to some key does not trigger revalidation.

The solution is to first remove the key that triggered IsValid to be false and recreate it and assign the value to it. When you do that the ModelState automatically revalidates and if everything is fine, IsValid returns true.

Like this:

bindingContext.ModelState.Remove("Slug");
    bindingContext.ModelState.Add("Slug", new ModelState());
    bindingContext.ModelState.SetModelValue("Slug", new ValueProviderResult(obj.Slug, obj.Slug, null));
mare
  • 13,033
  • 24
  • 102
  • 191
0

yes, it is allowed.

Why your view is showing the old values I cannot say based on your code. do you access

Model.Lastname

in your Viewcode in Create.aspx?

AndreasKnudsen
  • 3,453
  • 5
  • 28
  • 33
0

If you are using the Html helper to render your textboxes and bind to your model, e.g:

<%= Html.TextBox("toCreate.LastName", Model.Person.LastName) %>

When the page is being rendered from a POST, by default the helper renders the value that was sent in the POST data. I.e. it only uses Model.Person.LastName the first time (a GET). This is normally the preferred approach, but if you want to avoid this, just write the html yourself:

<input type="text" name="toCreate.LastName" value="<%= Html.Encode(Model.Person.LastName)" />
JonoW
  • 14,029
  • 3
  • 33
  • 31
  • i thought the controlling parts of program are all done basically in controller, that view and model are basically very lightweight players in the mvc triumvirate (especially with spark view) – Hao Mar 11 '10 at 12:09
  • This isn't about control, this is about how the value in the textbox is rendered (after a POST), which is the job of the view - at least thats what I'm guessing the cause of the problem is :) – JonoW Mar 11 '10 at 12:30