zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    影院售票系统
    返璞归真
    【C++】【STL】【map】基础知识干货
    书签-技术类
    正则表达式-正则表达式校验金额最多保留两位小数
    winCommand-cmd杀死进程
    idea快捷键-总结
    接口封装-泛型方法、泛型接口、lambda表达式【类似ios传递block】
    treeMap-get返回null,因为比较器出问题
    git-linux一个月更新80万行代码,如何保证项目稳健?
  • 原文地址:https://www.cnblogs.com/xiaoshi657/p/5747559.html
Copyright © 2011-2022 走看看