MVC中自带的异步((Ajax.BeginForm)无效

1、确定unobtrusive-ajax已经引用,VS2012带,2013不带

2、注意jq和unobtrusive-ajax引用顺序问题,确保jq在前

3、注意JQ和unobtrusive-ajax版本问题

    1.8以上的JQ要去nuget上下载较新的unobtrusive-ajax,1.8以下的用VS2012自带的即可,2013不带,

4、如果控制台提示TypeError: $(...).live is not a function

    说明JQ版本过低,JQ1.9更新了很多东西,其中live就被去掉了

引用:

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

ajax:

            @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>
            }
View Code

回调函数jq:

    function afterEdit(data) {
        if (data.State == 200) {
            location.reload();
        }
    }

controller:

        [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");
            }
        }
View Code
原文地址:https://www.cnblogs.com/xiaoshi657/p/5747559.html