1. MVC HtmlHelper method
1.
Html.BeginForm(actionName,controllerName, method,htmlAttributes){}2.
BeginRouteForm method (HtmlHelper, String, Object, FormMethod)
2. Traditional Form Form Aciton attribute submission
3. Jquery+Ajax submit form
4. MVC Controller controller and form parameter passing
MVC HtmlHelper method
1. Html.BeginForm(actionName,controllerName, method,htmlAttributes){}
Note: All content to be submitted, including buttons, must be within {}
parameter
htmlHelper type: System.Web.Mvc.HtmlHelper
This method extends the HTML helper instance. actionName type: System.String
The name of the operation method. controllerName type: System.String
The name of the controller. routeValues type: System.Object
An object containing routing parameters. By checking the properties of the object, the parameters are retrieved using reflection. This object is usually created using the object initializer syntax. method type: System.Web.Mvc.FormMethod
HTTP method (GET or POST) used to process forms. htmlAttributes type: System.Object
An object containing the HTML attributes to set for the element.
Return value
Type: System.Web.Mvc.Html.MvcForm
<form> start tag.
Usage instructions
In Visual Basic and C#, you can call this method as an instance method to any object of type HtmlHelper. When you call this method using instance method syntax, the first parameter is ignored
Html.BeginForm method example
MVC View Code
<h1>Apply online</h1>@using (Html.BeginForm("Apply", "Star", FormMethod.Post, new {@class="MyForm"})){<div><table><tr><td>Triver Type</td><td>@Html.DropDownListFor(m => m.StarModel.TypeID, Model.DropList, new { id = "type", @class = "my-" })</td></tr><tr><td>Home Page Expert Photo</td><td><div id="UploadPhoto"><a href="javascript:void(0);"><span><em>+</em>Upload the photo</span><input tabindex="3" size="3" name="pic" id="absFileInput" type="file" /></a></div></td></tr><tr><td></td><td>@Html.HiddenFor(m => m.StarModel.UserGravatar, new { id = "SXtPhoto" })<img src="" id="imgPhoto" /></td></tr><tr><td>Reasons for self-recommendation</td><td>@Html.TextAreaFor(m => m.StarModel.ApplyReason, new { id = "tDesc" })</td></tr><tr><td></td><td><a href=" javascript:void(0)" id="btnApplication"><img src="@Url.Content("~/Areas/SNS/Themes/Default/Content/images/ap_9.gif")" /></a></td></tr></table></div>}2. BeginRouteForm method (HtmlHelper, String, Object, FormMethod)
parameter
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 publish URL.
routeValues type: System.Object
An object containing routing parameters. By checking the properties of the object, the parameters are retrieved using reflection. This object is usually created using the object initializer syntax.
method type: System.Web.Mvc.FormMethod
HTTP method (GET or POST) used to process forms.
Return value
Type: System.Web.Mvc.Html.MvcForm
A start <form> tag.
Instructions for use
In Visual Basic and C#, this method can be called as an instance method on any object of type HtmlHelper. When calling this method using the instance method syntax, omit the first parameter.
BeginRouteForm Example
<div>@using (Html.BeginRouteForm("SearchPage", new { cityID = Model.CityID, productType = Model.CurrentProductType, currentPageIndex = Model.CurrentIndex, keyword = Model.keyword }, FormMethod.Get)){<input type="text" name="keyword" [email protected]><input type="submit" id="submit" value="Search" >}</div>Traditional Form Form Aciton Attribute Submission
Directly use the Aciton attribute of the html form to submit.
Method Example
<form id="askform" action="@Url.Action("AskForm")" method="post"><div><span></span><select id="dplBDTType" name="dplBDTType"></select> <select id="selType" name="selType"></select> </div></form>Jquery+Ajax Submit Form
Method Example
View section
<div id="postWeibo"><a href="javascript:void(0)">Publish</a></div>
Jquery and Ajax parts
//Posted a 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 >= 40) {ShowFailTip("Weibo title cannot exceed 40 words!");return;}//Check whether the number is if (isNaN(fee)) {ShowFailTip("Cannot contain text, must be numeric values!"); return;}if (ContainsDisWords(title + des)) {ShowFailTip('The content you entered contains a disable word, please re-enter!'); return;}$.ajax({url: "/fx" + $Maticsoft.BasePath + "Blog/AjaxUpdate",type: 'POST',async: 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 controller and form parameter passing
1. Normal parameters
HTML tag name is the same as parameter name.
public ActionResult AskForm(string txtTitle, string txtEditor, string dplBDTType, string selType, string txtYZM){}2. Entity transmission
The HTML tag name attribute and Model attribute are consistent
[HttpPost]public ActionResult Apply(ViewModel.SNS.Star model){//Logistic}4. Transfer parameters in the form collection
[HttpPost]public ActionResult Apply(FormCollection Form){//Logistic}The above is a detailed explanation of the 4 methods of submitting MVC forms introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!