2 Methods of form submission controller in ASP.NET.MVC and 4 methods of receiving page submission data from controller

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.