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
  • 相关阅读:
    Comet OJ
    BZOJ 4026: dC Loves Number Theory 可持久化线段树 + 欧拉函数 + 数学
    BZOJ 3887: [Usaco2015 Jan]Grass Cownoisseur tarjan + spfa
    BZOJ 3357: [Usaco2004]等差数列 动态规划
    BZOJ 3043: IncDec Sequence 差分 + 思维
    BZOJ 2288: 【POJ Challenge】生日礼物 贪心 + 堆 + 链表
    BZOJ 1776: [Usaco2010 Hol]cowpol 奶牛政坛 LCA + 树的直径
    爬虫模拟有道字典进行翻译,还发现了一条好玩的js
    django-orm框架表单的增删改查
    数据库的备份,迁移
  • 原文地址:https://www.cnblogs.com/xiaoshi657/p/5747559.html
Copyright © 2011-2022 走看看