Mvc Four Methods of Submitting Forms
- 2021-07-09 07:11:28
- OfStack
1. MVC HtmlHelper method
1.
Html.BeginForm(actionName,controllerName,method,htmlAttributes){}
2.
BeginRouteForm Method (HtmlHelper, String, Object, FormMethod)
2. Submission of Aciton attribute of traditional Form form
3. Jquery+Ajax Submit Form
4. MVC Controller controller and form parameter transfer
MVC HtmlHelper method
1. Html. BeginForm (actionName, controllerName, method, htmlAttributes) {}
Note: All content to submit, including buttons, must be in {}
Parameter
htmlHelper Type: System. Web. Mvc. HtmlHelper
This method extends an 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
1 object containing routing parameters. Retrieve parameters by checking the attributes of objects. This object is usually created using object initializer syntax. method Type: System. Web. Mvc. FormMethod
The HTTP method (GET or POST) used to process the form. htmlAttributes Type: System. Object
1 object containing the HTML attribute to be set for this element.
Return value
Type: System. Web. Mvc. Html. MvcForm
< form > Start tag.
Instructions for usage
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 instance method syntax, the first parameter is ignored
Html. BeginForm method example
MVC View code
<h1> Online application </h1>
@using (Html.BeginForm("Apply", "Star", FormMethod.Post, new {@class="MyForm"}))
{
<div class="application_b_3">
<table width="820" border="0">
<tr>
<td width="80" height="50"> Talent type </td>
<td width="730">
@Html.DropDownListFor(m => m.StarModel.TypeID, Model.DropList, new { id = "type", @class = "my-" })
</td>
</tr>
<tr>
<td height="50"> Home photo </td>
<td>
<div class="picture_an" id="UploadPhoto" style="width: 142px">
<a href="javascript:void(0);" class="btn_addPic"><span><em>+</em> Upload photos </span>
<input tabindex="3" title=" Support jpg , jpeg , gif , png Format, file less than 5M" size="3" name="pic" id="absFileInput" class="filePrew" type="file" />
</a>
</div>
</td>
</tr>
<tr>
<td height="50"></td>
<td>
@Html.HiddenFor(m => m.StarModel.UserGravatar, new { id = "SXtPhoto" })
<img src="" id="imgPhoto" height="176px" />
</td>
</tr>
<tr>
<td height="100"> Reasons for self-recommendation </td>
<td>
@Html.TextAreaFor(m => m.StarModel.ApplyReason, new { id = "tDesc" })
</td>
</tr>
<tr>
<td height="50"></td>
<td>
<a href=" javascript:void(0)" id="btnApplication"><img src="@Url.Content("~/Areas/SNS/Themes/Default/Content/images/ap_9.gif")" alt="" /></a>
</td>
</tr>
</table>
</div>
}
2. BeginRouteForm method (HtmlHelper, String, Object, FormMethod)
Parameter
htmlHelper Type: System. Web. Mvc. HtmlHelper
This method extends an HTML helper instance.
routeName Type: System. String
Gets the name of the route on which the form publishes URL.
routeValues Type: System. Object
1 object containing routing parameters. Retrieve parameters by checking the attributes of objects. This object is usually created using object initializer syntax.
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
1 start
<
form
>
Mark.
Instructions for use
In Visual, Basic and C #, you can call this method as an instance method on any object of type HtmlHelper. When this method is called using instance method syntax, omit the first parameter.
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 }, FormMethod.Get))
{
<input type="text" name="keyword" class="search-ipt" value=@Model.keyword>
<input type="submit" id="submit" value=" Search Cable " class="gsearch-btn" >
}
</div>
Traditional Form Form Aciton Attribute Submission
Submit directly using the Aciton property of the html form.
Method sample
<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 Submit Form
Method sample
View Part
<div class="issue" id="postWeibo" style="width: 80px">
<a href="javascript:void(0)" class="publish-btn"> Publish </a>
</div>
Jquery and Ajax sections
// Publish 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 can't exceed 40 A word! ");
return;
}
// Check whether it is a number
if (isNaN(fee)) {
ShowFailTip(" Cannot contain text must be numeric! ");
return;
}
if (ContainsDisWords(title + des)) {
ShowFailTip(' The content you entered contains disabled words, 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(" Operation failed, please try again! ");
} else if (resultData == "AA") {
$.jBox.tip(' Administrator cannot operate ', 'error');
} else {
var data = $(resultData);
}
}
});
});
MVC Controller Controller and Form Parameter Passing
1. Common parameters
HTML tag name and parameter name 1 sample.
public ActionResult AskForm(string txtTitle, string txtEditor, string dplBDTType, string selType, string txtYZM)
{
}
2. Substantial referencing
HTML tag name attribute and Model attribute remain 1 to
[HttpPost]
public ActionResult Apply(ViewModel.SNS.Star model)
{
// Logic
}
4. Form collection transfer
[HttpPost]
public ActionResult Apply(FormCollection Form)
{
// Logic
}