4 ways to submit a form in MVC all in one full explanation

Source: Internet
Author: User
Tags html form instance method tag name

One, MVC HtmlHelper method
    1. Html.BeginForm (actionname,controllername,method,htmlattributes) {}
    2. Beginrouteform method (HtmlHelper, String, Object, FormMethod)
Second, the traditional form form Aciton attribute submission Three, Jquery+ajax submit form four, MVC controller controllers and form parameters pass MVC HtmlHelper method

One, Html.BeginForm (actionname,controllername,method,htmlattributes) {}

Note: All content to be submitted including the button must be within {}

Parameters
HtmlHelper
Type: System.Web.Mvc.HtmlHelper
This method extends the HTML helper instance.
ActionName
Type: System.String
the name of the action method.
Controllername
Type: System.String
the name of the controller.
Routevalues
Type: System.Object
An object that contains the route parameters. The parameters are retrieved using reflection by examining the properties of the object. This object is typically created by using the object initializer syntax.
Method
Type: System.Web.Mvc.FormMethod
the HTTP method (GET or POST) used to process the form.
Htmlattributes
Type: System.Object
An object that contains the HTML attributes to set for the element.
Return value type: System.Web.Mvc.Html.MvcForm
<form> start tag. Usage Notes

In Visual Basic and C #, you can call this method as an instance method on any object of type HtmlHelper. When you call this method using an instance method syntax, the first argument is ignored

Html.BeginForm Method Example

MVC View Code

 Two, Beginrouteform method (HtmlHelper, String, Object, FormMethod) parameters
 
 
HtmlHelper
Type: System.Web.Mvc.HtmlHelper
This method extends the HTML helper instance.
RouteName
Type: System.String
The name of the route used to get the form publishing URL.
routevalues
type: System.Object
An object that contains the route parameters.  
Method
Type: System.Web.Mvc.FormMethod
The HTTP method (GET or POST) used to process the form.
Return value type: System.Web.Mvc.Html.MvcForm
a start <form> tag.
Instructions for use

In Visual Basic and C #, you can call this method as an instance method on any object of type HtmlHelper. Omit the first argument when calling this method using the instance method syntax.

Beginrouteform Example
<div class= "Group-search-box clearfix" >                      @using (Html.beginrouteform ("Searchpage", new {Cityid = Model.cityid, ProductType = model.currentproducttype, CurrentPageIndex = model.currentindex, keyword = Model.keyword}, F Ormmethod.get)           {                <input type= "text" name= "keyword" class= "search-ipt" [Email protected]>                < Input type= "Submit" id= "submit" value= "search" class= "gsearch-btn" >           }                        </div>
Traditional form Form Aciton attribute submission

Submit directly using the Aciton property of the HTML form.

Method Example

<form id= "Askform" action= "@Url. Action (" Askform ")" method= "POST" >                    <div class= "Title-area-outter Clearfix ">                        <span></span>                        <select id=" Dplbdttype "name=" Dplbdttype "></select>                                           <select id= "SelType" name= "SelType" ></select>                       </div></form>
Jquery+ajax submitting a Form Method Example

View section

  <div class= "Issue" id= "Postweibo" style= "width:80px" >                            <a href= "javascript:void (0)" class= "Publish-btn" > Publishing </a>                        </div>

jquery and Ajax Sections

    Publish Long Weibo $ ("#postWeibo"). Click (function () {var BlogID = $ ("#hfID"). Val ();        var title = $ ("#title"). Val ();        var Imgurl = $ ("#previewImgHide"). Val ();        var des = editor.getcontent (); if (title = = "") {Showfailtip (' Weibo title cannot be empty!            ‘);        Return } if (Title.length >=) {showfailtip ("Weibo title cannot exceed 40 characters!            ");        Return }//Check if the number if (IsNaN (fee)) {Showfailtip ("cannot contain text must be numeric!            ");        Return } if (Containsdiswords (title + des)) {showfailtip (' The content you entered contains a disabled word, please re-enter!            ‘);        Return } $.ajax ({url: "/fx" + $Maticsoft. BasePath + "Blog/ajaxupdate", type: ' POST ', asy Nc:false, DataType: ' HTML ',//timeout:10000, data: {title:title, Cityid: City, Fee:fee, Categoryid:category, Days:days, Tag:tag, Startdate:startdate, Enddate:enddate, Imgurl:imgurl, des:dES, Blogid:blogid},//Success:function (Resultdata) {$ (". Dialogdiv"). Hide (); if (Resultdata = = "No") {Showfailtip ("The operation failed, please try again!")                ");                } else if (Resultdata = = "AA") {$.jbox.tip (' admin cannot operate ', ' error ');                               } else {var data = $ (resultdata);    }            }        }); });
MVC controller controllers and form parameter passing 1. General parameters

The HTML tag name is the same as the parameter name.

Public ActionResult Askform (string txttitle, String txteditor, String Dplbdttype, String SelType, String txtyzm)        {}

2. Entity Transfer

The HTML Tag Name property is consistent with the model property

[HttpPost]        Public ActionResult Apply (ViewModel.SNS.Star model)        {//logic}
4. Form collection Parameters
[HttpPost]        Public ActionResult Apply (formcollection Form)        {//logic}

4 ways to submit a form in MVC all in one full explanation

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.