tags:

views:

79

answers:

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: http://stackoverflow.com/questions/707569/asp-net-mvc-changing-models-properties-on-postback and here: http://stackoverflow.com/questions/2588588/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);
}
A: 

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
nope, i access toCreate.Lastname from the class that inherited Controller
Hao
A: 

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
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
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
+2  A: 

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