Pages

Thursday, November 29, 2018

MVC link for quick learning

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 
1.
  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 name = frmobj["userid"];  
  16. string password = frmobj["pwd"];  
  17. if (name == "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. }  


  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. }  


2.

  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. }  

  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. }  
3.
  1. namespace MVCActionMethodParameters.Models  
  2. {  
  3. public class Login  
  4. {  
  5. public string userid { getset; }  
  6. public string pwd { getset; }  
  7. }  
  1. namespace MVCActionMethodParameters.Models  
  2. {  
  3. public class Login  
  4. {  
  5. public string userid { getset; }  
  6. public string pwd { getset; }  
  7. }  

  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" />  

No comments:

Post a Comment