views:

285

answers:

2

Hello fellow programmer

I came from PHP language(codeigniter), but now I learning ASP.Net MVC :)

In PHP codeigniter we can catch the post variable easily with

$this->input->post("theinput");

I know that in ASP.Net MVC we can create an action method that will accepts variable from post request like this

public  ActionResult Edit(string  theinput)

Or by

public ActionResult Edit(FormCollection formCol)

Is there a way to catch post variable in ASP.Net like PHP's codeigniter, so that we don't have to write FormCollection object nor have to write parameter in the action method (because it can get very crowded there if we pass many variable into it)

Is there a simple getter method from ASP.Net to catch these post variables?

edited: I'd be very thankful if u can give me a link to these tutorials :D

A: 

Yes.

Request.Form["theinput"]

However, the best way to do this is to make a model class that contains properties for each variable you need to access, then make your action take an instance of that class as a parameter.

SLaks
I'm a beginner at .Net i do not really understand what a model class is. Can u give me a link to these tutorials :D
Erwin
http://www.asp.net/learn/mvc/
SLaks
+1  A: 

You typically don't want to use FormCollection or Request.Form in your code. These objects are very hard to mock, which makes automated testing very difficult.

Best practice is to create a viewmodel class with all the input you need as properties and take this class as input to your controller action.

gautema
Actually, because of `System.Web.Abstractions`, they're not so hard to mock. However, it certainly is better to take a model class.
SLaks
I'm sorry i do not understand what a viewmodel class is. Can u give me a link to these tutorials :D
Erwin
A viewmodel is a model specialised to fit the view. Look at resources like: http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx and http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx
gautema