3

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

strike_noir
  • 3,868
  • 11
  • 50
  • 92

2 Answers2

3

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
  • 800,742
  • 167
  • 1,811
  • 1,896
  • 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 – strike_noir Mar 24 '10 at 00:24
3

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
  • 637
  • 6
  • 14
  • Actually, because of `System.Web.Abstractions`, they're not so hard to mock. However, it certainly is better to take a model class. – SLaks Mar 23 '10 at 12:01
  • I'm sorry i do not understand what a viewmodel class is. Can u give me a link to these tutorials :D – strike_noir Mar 24 '10 at 00:23
  • 1
    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 Mar 24 '10 at 10:14