views:

27

answers:

2

Hello,

Im starting with MVC2 and i have a simple question:

If i have a typed view with a form inside, and this textbox created with lambda expressions:

 <%: Html.TextBoxFor(e => e.Name)%>

When i submit this form the default model binder take the request form, takes the model typed to the view, serialize the data posted (as a this model) and pass it to an action of my controller.

To try to explain myself better, lets imagine that i have a url like localhost/edittestmodel/ID/1 and i have in my controller action the following code:

public ActionResult Edit(int id)
{
    TestModel testmodel=new TestModel();
    testmodel.Name="texttorenderintotextbox";
    //whats the class that place the testmodel properties into the view? 
    return View(testmodel);

}

What's the responsable class for place the Name property of my testmodel object into the textbox

<%: Html.TextBoxFor(e => e.Name)%>

Thanks in advance.

Best Regards.

Jose.

+1  A: 

It's the TextBoxFor helper method that's responsible for generating the input field from the lambda expression.

Darin Dimitrov
Thanks Darin.Kind Regards.
Josemalive
A: 

View's don't have anything to do in POST request and model binding

When you have strong type views, model type is barely used to have the simplicity of code intellisense in view's code (so you can use lambda expressions like in your example).

But when you POST data back, nothing gets checked against the view. It gets checked against controller action parameters though. And if there's a parameter with a certain custom model binder type, that particular model binder is used to process incoming data.

But to answer your question: TextBoxFor checks your strong type view's model and generates particular textbox with correct name attribute. So data from it will be posted back under the correct form field name.

To go even deeper. It's the view engine that parses view's ASPX code and runs all server side scripts including Html.TextBoxFor() call.

Robert Koritnik