tags:

views:

710

answers:

1

Hi,

I've been playing with ASP.NET MVC and ran into something I can't figure out.

Suppose I have an object like this :

public class TestObject
{
    public string Name { get; set; }
    public int Age { get; set; }
}

And a view page (Create.aspx) like this :

<form action="/Create" method="post">
    <p>
        <%=Html.TextBox("Name") %>
    </p>
    <p>
        <%=Html.TextBox("Age")%>
    </p>
</form>

And on my controller I have these actions :

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
    return View(new TestObject { Name = "DefaultName", Age = 10 } );
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(TestObject o)
{
    o.Name = "ChangedNameToSomethingElse";
    o.Age = 15;
    return View(o);
}

The Html.TextBox() method always genereates the textboxes with the default values, even after the postback, where the object is passed back with different properties on its values. Now, granted, I can't think of a real world example why I'd want to do such a thing but I still don't understand why I always end up having textboxes populated with the model's values that were set on the Create action with the AcceptVerbs(HttpVerbs.Get) attribute.

Note : I've tried Html.TextBox("Name", Model.Name) but the result is still the same. And I verified that the Create action with AcceptVerbs(HttpVerbs.Post) actually runs, by passing a value via ViewData to the View. Also, the udated value is displayed when I output the value with <%=Model.Name %> but again, not on the textbox.

Is there something obvious I'm missing, or is there a reasoning behind this behaviour?

+5  A: 

If you bind the result of a post request through the declaration of the method or by UpdateModel or TryUpdateModel to an object such as TestObject, a property called ModelState will get filled in with these values. The HTML helpers such as Textbox will always bind to modelstate over an explicitly passed model object.

Thanks for your answer. Do you also happen to know the correct and the suggested way to re-populate the ModelState, by giving it an object maybe? Or should I manually give the keys and the values to it, after clearing the collection?
çağdaş
I just checked. You can achieve what you want by calling ModelState.Clear() in your Create action with the Post attribute.
Yep, looks like it works. Thank you for your help again. I will leave the question open for a while in case someone knows a different method that could also be useful.
çağdaş
Sure. Thanks for asking your question: I've learnt a bit more tonight too.
Thanks, this just helped me aswell. As for a real world application which you mention -- I am writing an interface which had lists in the model, and some of the http post commands will remove items from the list. In this case, you need to use the model values as myList[1] for example in the ModelState will be different once you have removed an item, but MVC will bind incorrect values when creating the text boxes.
amarsuperstar