MVC下HtmlHelper自带BeginForm表单提交与异步Ajax请求
生活随笔
收集整理的這篇文章主要介紹了
MVC下HtmlHelper自带BeginForm表单提交与异步Ajax请求
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
假如有一個(gè)數(shù)據(jù)表格UserInfo:
public class UserInfo {public int Id { get; set; }public string Name { get; set; }public string Pwd { get; set; }public int Sex { get; set; } }控制器下Action方法:UserInfos
表單提交過來后UserInfo user對(duì)象可以獲取到value值,只要html標(biāo)簽的name值與UserInfo下的屬性名相同
public ActionResult UserInfos(UserInfo user) {if (user.Name != "admin" && user.Pwd != "123"){return Content("用戶名或密碼錯(cuò)誤");}else{return Content("登錄成功");} }html代碼(表單提交):
@using (Html.BeginForm("UserInfos", "Home", FormMethod.Post)) {<input type="text" name="Id" value="" /><input type="text" name="Name" value="" /><input type="text" name="Pwd" value="" /><select name="Sex"><option value="-1">請(qǐng)選擇</option><option value="1">男</option><option value="0">女</option></select><input type="submit" id="btnOK" value="提交" /> }html代碼(異步表單表單提交):
@using (Ajax.BeginForm("UserInfos", "Home", new AjaxOptions() { Confirm = "您確定要提交嗎?", HttpMethod = "post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "result", LoadingElementId = "loading", OnSuccess = "afterSuccess", OnFailure = "afterError" })) {<input type="text" name="Id" value="" /><input type="text" name="Name" value="" /><input type="text" name="Pwd" value="" /><select name="Sex"><option value="-1">請(qǐng)選擇</option><option value="1">男</option><option value="0">女</option></select><input type="submit" id="btnOK" value="提交" /> } <span style="color:red;font-size:10px;" id="result"></span><div id="loading" style="display:none;"><img src="~/Content/loading.jpg" /></div>上面的InsertionMode = InsertionMode.Replace為追加數(shù)據(jù)
UpdateTargetId = "result"為請(qǐng)求返回的數(shù)據(jù)填充到這個(gè)ID值
OnSuccess = "afterSuccess"和 OnFailure = "afterError"為成功與失敗要執(zhí)行的JS函數(shù)
轉(zhuǎn)載于:https://www.cnblogs.com/genesis/p/5247711.html
總結(jié)
以上是生活随笔為你收集整理的MVC下HtmlHelper自带BeginForm表单提交与异步Ajax请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring 中属性scope 的pro
- 下一篇: Java与C/C++的比较(转)