asp.net mvc post variable to controller
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 开发者_如何学Goobject 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
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.
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.
精彩评论