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
  • 相关阅读:
    Vue创建三:组件间bus传值
    vue创建二:引入本地图片
    Vue创建一:创建项目及样式引入
    jQuery源码解析之on事件绑定
    浏览器的同源策略与跨域处理
    常见的contentType编码类型【转】
    CSS预处理器Sass -- sass的基本语法(4)
    CSS预处理器Sass -- Sass工程的创建及sass文件编译(3)
    CSS预处理器Sass -- Sass、Less、Stylus比较(2)
    CSS预处理器Sass -- Sass、compass初识及其安装(1)
  • 原文地址:https://www.cnblogs.com/xiaoshi657/p/5747559.html
Copyright © 2011-2022 走看看