views:

75

answers:

1

My model has a property of "output" and my form has a TextBox named "output", both spelled exactly the same. When I fill out the form and post it, the returning view has unexpected results.

Specifically, I receive the posted "output" form variable which is then bound to my Model in the Controller Method, then I change this variable to a different value before I pass the Model back to the view.

the posted output property is "one thing" my Model property is set to "another"

I have this in my code: <%= Html.TextBox("output") %>

Which renders to this: <input id="output" name="output" type="text" value="one thing" />

However, when debugging, the Model does contain the correct value ("another").

Its using the posted value rather than the value assigned in the controller. Any ideas why and how to fix it?

+2  A: 

Assuming you're using a strongly typed view like so:

<%@ Page ... Inherits="System.Web.Mvc.ViewPage<...TestModel>" %>

And having an EditorFor replacing your

<%= Html.TextBox("output") %> like so <%= Html.EditorFor(p=>p.output) %>

you can do in your Controller

ModelState.SetModelValue("output", new ValueProviderResult("Some string", string.Empty, new CultureInfo("en-US")));

A similar question has been asked here

Carlos Fernandes