Action Method Parameters In ASP.NET MVC

Action Method Parameters
  • We can organize the action methods for GET and POST requests separately.
  • We can easily create seperate action methods for each request types.
Request Type Attribute
  • [HttpGet]
  • [HttpPost]
Action method parameters
 
MVC framework provides 3 different action methods for Post Request, which are given below.
  1. Form Collection
  2. Formal Parameters
  3. Model Class Object 
i) Form Collection
  • It is a pre-defined class in syste.web.mvc namespace.
  • It stores the items in key value pairs format only.
    • key - name of the input fields.
    • value - input content field (string type).
Step 1 - Create a new empty MVC Application. 
 
Step 2 - Add the controller, as shown below. 
 
 
 
Step 3 - First is controller example of FormCollection. 
  1. using System.Web.Mvc;  
  2. namespace MVCActionMethodParameters.Controllers  
  3. {  
  4. public class HomeController : Controller  
  5. {  
  6. // GET: Home  
  7. [HttpGet]  
  8. public ActionResult Index()  
  9. {  
  10. return View();  
  11. }  
  12. [HttpPost]  
  13. public ActionResult Index(FormCollection frmobj) //FormCollection  
  14. {  
  15. string username = frmobj["userid"];  
  16. string password = frmobj["pwd"];  
  17. if (username == "Admin" && password == "123456")  
  18. {  
  19. Response.Write("<h2> Success </h2> Valid User...");  
  20. }  
  21. else  
  22. Response.Write(" <h2> Failed </h2> Invalid User...");  
  23. return View();  
  24. }  
  25. }  
Step 4 - Right click on FormCollection Method and add view. Afterwards, create login fieldsm as shown below.
  1. @{  
  2. ViewBag.Title = "Index";  
  3. }  
  4. <h1>Form Collection</h1>  
  5. <h2>Login </h2>  
  6. @using (Html.BeginForm())  
  7. {  
  8. <label>User Name</label>  
  9. <input type="text" class="form-control" id="userid" name="userid" />  
  10. <label>Password</label>  
  11. <input type="text" class="form-control" id="pwd" name="pwd" /> <br />  
  12. <input type="submit" class="btn btn-success" value="Submit" />  
  13. }  
ii) Formal Parameters 
  • Using this option, we can get the values easily in the form of pre-defined data types example like int, string etc.
  • We can easily handle type casting.
  • It will read based on the form input fields name directly.
Note - The name of the parameters should be the same as the input field name. Afterwards, it will possible to achieve the formal parameters.
 
Step 5 - Again add another controller, give any name of the controller and paste the code given below into Controller.
  1. using System.Web.Mvc;  
  2. namespace MVCActionMethodParameters.Controllers  
  3. {  
  4. public class LoginController : Controller  
  5. {  
  6. // GET: Login  
  7. [HttpGet]  
  8. public ActionResult Login()  
  9. {  
  10. return View();  
  11. }  
  12. [HttpPost]  
  13. public ActionResult Login(string userid, string pwd) //Formal Parameters  
  14. {  
  15. string username = userid;  
  16. string password = pwd;  
  17. if (username == "Admin" && password == "123456")  
  18. {  
  19. Response.Write("<h2> Success </h2> Valid User...");  
  20. }  
  21. else  
  22. Response.Write(" <h2> Failed </h2> Invalid User...");  
  23. return View();  
  24. }  
  25. }  
  26. }  
Step 6 - Right click on Formal paratmeter action method and add same login fields in View. 
  1. @{  
  2. ViewBag.Title = "Login";  
  3. }  
  4. <h1>Formal Parameters</h1>  
  5. <hr />  
  6. <h2>Login</h2>  
  7. @using (Html.BeginForm())  
  8. {  
  9. <label>User Name</label>  
  10. <input type="text" class="form-control" id="userid" name="userid" />  
  11. <label>Password</label>  
  12. <input type="text" class="form-control" id="pwd" name="pwd" /> <br />  
  13. <input type="submit" class="btn btn-success" value="Submit" />  
  14. }  
iii ) Model class object
  • It is the same as business entities.
  • We have created a seperate class for the objects in model folder. 
  • In MVC model, logic is prepared by using the classes, which are based on the database tables.
  • It requires model class objects.
Note - The name of the parameters should be same as the input field name, then only it will possible to achieve formal parameters. 
 
Step 7 - Right click on Model folder and add the class.
 
 
Step 8 - Create a class with the objects.
  1. namespace MVCActionMethodParameters.Models  
  2. {  
  3. public class Login  
  4. {  
  5. public string userid { getset; }  
  6. public string pwd { getset; }  
  7. }  
  8. }  
Step 9 - Again add another controller for Model Class Object and write the code given below.

  1. using System.Web.Mvc;  
  2. using MVCActionMethodParameters.Models;  
  3. namespace MVCActionMethodParameters.Controllers  
  4. {  
  5. public class LoginModelController : Controller  
  6. {  
  7. // GET: LoginModel  
  8. public ActionResult LoginModel()  
  9. {  
  10. return View();  
  11. }  
  12. [HttpPost]  
  13. public ActionResult LoginModel(Login obj) // Model class object  
  14. {  
  15. string username = obj.userid;  
  16. string password = obj.pwd;  
  17. if (username == "Admin" && password == "123456")  
  18. {  
  19. Response.Write("<h2> Success </h2> Valid User...");  
  20. }  
  21. else  
  22. Response.Write(" <h2> Failed </h2> Invalid User...");  
  23. return View();  
  24. }  
  25. }  
  26. }  
Step 10 - Right click on Model class object method and add the view.
  1. <h1>Formal Parameters</h1>  
  2. <hr />  
  3. <h2>Login</h2>  
  4.    
  5. @using (Html.BeginForm())  
  6. {  
  7. <label>User Name</label>  
  8. <input type="text" class="form-control" id="userid" name="userid" />  
  9. <label>Password</label>  
  10. <input type="text" class="form-control" id="pwd" name="pwd" /> <br />  
  11. <input type="submit" class="btn btn-success" value="Submit" />  
  12. }  
Step 11 - Run the Application. Enter a valid userid and password. Now, you will see the output.