一、肯定unobtrusive-ajax已經引用,VS2012帶,2013不帶html
二、注意jq和unobtrusive-ajax引用順序問題,確保jq在前jquery
三、注意JQ和unobtrusive-ajax版本問題ajax
1.8以上的JQ要去nuget上下載較新的unobtrusive-ajax,1.8如下的用VS2012自帶的便可,2013不帶,ide
四、若是控制檯提示TypeError: $(...).live is not a function函數
說明JQ版本太低,JQ1.9更新了不少東西,其中live就被去掉了post
引用:spa
<script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
ajax:code
@using (Ajax.BeginForm("EditeArticleType", "UArticleType", new { }, new AjaxOptions() { HttpMethod = "post", OnSuccess = "afterEdit" }, new { id = "addForm" })) { <div class="modal-body"> <div class="form-group"> <label for="recipient-name" class="control-label">分類名稱:</label> <input name="TypeName" type="text" class="form-control" id="type-name"> </div> <div class="form-group"> <label for="recipient-name" class="control-label">排序:</label> <input name="ListIndex" type="text" class="form-control" id="type-index"> </div> <div class="form-group"> <label for="message-text" class="control-label">描述:</label> <textarea class="form-control" id="type-describe"></textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button> <button type="submit" class="btn btn-primary">肯定</button> </div> }
回調函數jq:orm
function afterEdit(data) {
if (data.State == 200) {
location.reload();
}
}
controller:htm
[HttpPost] public ActionResult EditeArticleType(ArticleType articleType) { articleTypeService = new BLL.ArticleTypeService(); if (!string.IsNullOrEmpty(articleType.TypeName)) { articleType.MemberID = CurrentMember.ID; articleType.IsDel = 0; articleType.CreateTime = DateTime.Now; var newType = articleTypeService.AddEntity(articleType); if (newType != null) { return Json (new ViewMessage {State=200,Msg="成功",Data=null});//RedirectToAction("UArticleType", "UserCenter"); } else { return View("/Views/Shared/Error.cshtml"); } } else { ViewBag.msg = "請求參數有誤"; return View("/Views/Shared/Error.cshtml"); } }