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.
- Form Collection
- Formal Parameters
- Model Class Object
1.
- using System.Web.Mvc;
- namespace MVCActionMethodParameters.Controllers
- {
- public class HomeController : Controller
- {
- // GET: Home
- [HttpGet]
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Index(FormCollection frmobj) //FormCollection
- {
- string name = frmobj["userid"];
- string password = frmobj["pwd"];
- if (name == "Admin" && password == "123456")
- {
- Response.Write("<h2> Success </h2> Valid User");
- }
- else
- Response.Write(" <h2> Failed </h2> Invalid User");
- return View();
- }
- }
- @{
- ViewBag.Title = "Index";
- }
- <h1>Form Collection</h1>
- <h2>Login </h2>
- @using (Html.BeginForm())
- {
- <label>User Name</label>
- <input type="text" class="form-control" id="userid" name="userid" />
- <label>Password</label>
- <input type="text" class="form-control" id="pwd" name="pwd" /> <br />
- <input type="submit" class="btn btn-success" value="Submit" />
- }
2.
- using System.Web.Mvc;
- namespace MVCActionMethodParameters.Controllers
- {
- public class LoginController : Controller
- {
- // GET: Login
- [HttpGet]
- public ActionResult Login()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Login(string userid, string pwd) //Formal Parameters
- {
- string username = userid;
- string password = pwd;
- if (username == "Admin" && password == "123456")
- {
- Response.Write("<h2> Success </h2> Valid User...");
- }
- else
- Response.Write(" <h2> Failed </h2> Invalid User...");
- return View();
- }
- }
- }
- @{
- ViewBag.Title = "Login";
- }
- <h1>Formal Parameters</h1>
- <hr />
- <h2>Login</h2>
- @using (Html.BeginForm())
- {
- <label>User Name</label>
- <input type="text" class="form-control" id="userid" name="userid" />
- <label>Password</label>
- <input type="text" class="form-control" id="pwd" name="pwd" /> <br />
- <input type="submit" class="btn btn-success" value="Submit" />
- }
3.
- namespace MVCActionMethodParameters.Models
- {
- public class Login
- {
- public string userid { get; set; }
- public string pwd { get; set; }
- }
- }
- namespace MVCActionMethodParameters.Models
- {
- public class Login
- {
- public string userid { get; set; }
- public string pwd { get; set; }
- }
- }
- <h1>Formal Parameters</h1>
- <hr />
- <h2>Login</h2>
- @using (Html.BeginForm())
- {
- <label>User Name</label>
- <input type="text" class="form-control" id="userid" name="userid" />
- <label>Password</label>
- <input type="text" class="form-control" id="pwd" name="pwd" /> <br />
- <input type="submit" class="btn btn-success" value="Submit" />
- }
No comments:
Post a Comment