How is form form submitted in MVC? How are controller controllers received?
1. cshtml Page Form submission
(1) The submission of the ordinary way
(2) Special way to submit
2, the controller handles the form data four kinds of methods
Method 1: Use the traditional request data
Method 2:action parameter name corresponds to table cell name value one by one
Method 3: Read from the MVC encapsulated formcollection container
Method 4: Use the entity as the action parameter to pass in, provided that the submitted form element name corresponds to the entity attribute name one by one
Controller source Code
Using Mvcstudy.models;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Namespace Mvcstudy.controllers
{
public class Logincontroller:controller
{
Public ActionResult Index () {
return View ();
}
Public ActionResult Main () {
return View ();
}
1. Form Request for cshtml page
<form action= "Login/index" method= "POST" ></form>
2, the Controller handles the form submission method four kinds of methods
Method 1: Use the traditional request data
[HttpPost]
Public ActionResult getusermsg ()
//{
String usercode = request.form["Usercode"];
String userpwd = request.form["Userpwd"];
if (usercode! = "Admin" | | userpwd! = "Admin") {
Return Redirecttoaction (".. /home/error ");
// }
Return Redirecttoaction ("main");
//}
Method 2:action parameter name corresponds to table cell name value one by one
[HttpPost]
Public ActionResult getusermsg (string usercode, String userpwd)
//{
string usercode = Usercode;
string userpwd = Userpwd;
if (usercode! = "Admin" | | userpwd! = "Admin") {
Return Redirecttoaction (".. /home/error ");
// }
Return Redirecttoaction ("main");
//}
Method 3: Read from the MVC encapsulated formcollection container
[HttpPost]
Public ActionResult getusermsg (formcollection form)
//{
String usercode = form["Usercode"];
String userpwd = form["Userpwd"];
if (usercode! = "Admin" | | userpwd! = "Admin")
// {
Return Redirecttoaction (".. /home/error ");
// }
Return Redirecttoaction ("main");
//}
Method 4: Use the entity as the action parameter to pass in, provided that the submitted form element name corresponds to the entity attribute name one by one
[HttpPost]
Public ActionResult getusermsg (sysuser user)
{
String usercode = user. Usercode;
String userpwd = user. Userpwd;
if (usercode! = "Admin" | | userpwd! = "Admin")
{
Return Redirecttoaction (".. /home/error ");
}
Return Redirecttoaction ("main");
}
}
}
Note: redirecttoaction ("main") is the page that redirects you to sign in, Redirecttoaction ("error") is the error prompt page
Entity Model Class Sysuser
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Namespace Mvcstudy.models
{
public class Sysuser
{
public string Usercode {get; set;}
public string Userpwd {get; set;}
Public Sysuser () {}
Public Sysuser (String usercode,string userpwd) {
Usercode = this. Usercode;
Userpwd = this. Userpwd;
}
}
}
Effect of landing page
Login Success Page
Login Failed page
Note: When login fails, you can also redirect to the previous login page
The 2 methods of the form form submission controller in the
Asp.net.mvc and the 4 ways that the controller receives the page submission data