tags:

views:

30

answers:

2

Hi, I am learning ASP.NET MVC, and ran across a video on the asp.net/mvc website that showed how to retrieve a value from a textbox after a postback. In the video the author simply grabs the value from the Request object in the controller.

It seems like this breaks the separation of concerns concept? By doing this the controller is now dependent upon the presence of a Request object which won't exist if one runs unit tests against the controller.

So I assume this is an incorrect way of retrieving form data on a postback. What is the correct way? Once I am in my controller, how do I get access to the postback data?

It seems there should be some intermediate step that essentially pulls the data from the postback and packages it into a nice object or some other format that the controller would then used?

+2  A: 

The data should be posted back to your Model or ViewModel. Your controller method that handles the POST will expect the model to be provided as a parameter.

Here is a blog entry that gives an example

Justin
+1  A: 

Using model binding, MVC can populate data coming from the form data, the query sting, cookies, and a number of other sources directly into your object model or other paramters defined as parameters to your action methods in the controller.

There's too many details of how this works to summarize here, but it is the cornerstone of the power of ASP.NET MVC.

Check out Models and Validation in ASP.NET MVC as a good starting point. You'll find tons of other resources around MVC model binding out there.

I've really liked Steven Sanderson's Pro ASP.NET MVC 2 Framework if you prefer physical books.

Reed Rector