部分视图在返回更新的部分视图时未获得更新。我错过了什么?

时间:2015-10-29 06:40:59

标签: c# jquery ajax asp.net-mvc

我的Html

$("#selectedMenu").change(function () {
                mnid = $(this).val();
                userId = $("#userid").val();
                $.ajax({
                    type: 'POST',
                    url: "/Users/getsubmenus",
                    data: "{userid:'" + userId + "',menuid:'" + mnid + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function (data) {
                        if (data.result == "Error") {
                            alert(data.message);
                        }
                    }
                });

            });

选择菜单上的AjaxMethod已更改

public ActionResult getsubmenus(string userid, string menuid)
        {            
            userright _userright = new userright();
            user _user = _ftwCommonMethods.GetuserDataById(Convert.ToInt32(userid));
            UserRightPartialView _UserRightPartialView = new UserRightPartialView();
            if (_user!=null)
            {
                IEnumerable<SubMenuListModel> _SubMenuListModel = _ftwCommonMethods.GetSubMenuItemsByMenu(_user.id.ToString(), menuid);

                _UserRightPartialView.firstname = _user.firstname;
                _UserRightPartialView.userid = _user.id;
                _UserRightPartialView._SubMenuListModel = _SubMenuListModel;
            }
            return PartialView("_userRightPartial", _UserRightPartialView);
        }

在Ajax中调用的操作结果“getsubmenus”

        [HttpPost]
        [AuthorizeUser]
        public ActionResult userrights(FormCollection form_collection)
        {
            IEnumerable<MenuListModel> _MenuListModel = _ftwCommonMethods.GetMenuItems();
            int userid = 0;
            if (!string.IsNullOrEmpty(form_collection["id"].ToString()) && form_collection["id"].ToString() != "0")
            {
                userid = Convert.ToInt32(form_collection["id"].ToString());
            }
            user _user = _ftwCommonMethods.GetuserDataById(userid);
            UserRightViewSearch _UserRightViewSearch = new UserRightViewSearch();
            _UserRightViewSearch._menu = _MenuListModel;
            if (_user != null)
            {
                IEnumerable<SubMenuListModel> _SubMenuListModel = _ftwCommonMethods.GetSubMenuItems(_user.id.ToString());
                UserRightPartialView _UserRightPartialView = new UserRightPartialView();
                _UserRightPartialView.firstname = _user.firstname;
                _UserRightPartialView.userid = _user.id;
                _UserRightViewSearch.id = _user.id;
                _UserRightPartialView._SubMenuListModel = _SubMenuListModel;
                _UserRightViewSearch._UserRightPartialView = _UserRightPartialView;
            }
            else
            {
                ViewData["message"] = "User Not Found.";
            }
            return View(_UserRightViewSearch);

        }

ActionResult当我点击搜索按钮

@model FTW_Admin.Models.UserRightPartialView

@if (Model.firstname != null)
{
    <div class="dhe-example-section" id="ex-1-2">
        <div id="example-1-2">
            <div class="col-md-6" style="padding:0px 10px 10px 0px">
                <div class="drag_to">@Model.firstname
                    @Html.Hidden("userid", Model.userid, new { @id = "userid" })
                </div>
                <div class="middle_section">
                    <div class="inner_section_page_sec">
                        <div id="container" class="main_page_area">
                            <div class="column left first">
                                <div>
                                    <ul class="sortable-list-allow">

                                            @foreach (var row in Model._SubMenuListModel)
                                            {
                                                if (row.allowd)
                                                {
                                                    <li class="sortable-item loop_content">
                                                        <table id="dragable_grid">
                                                            <tr>
                                                                <th style="color:#000">
                                                                    @row.submenuname
                                                                    @Html.Hidden("submenuid", @row.submenuid, new { @id = "submenuid" })
                                                                    @Html.Hidden("menuid", @row.submenuid, new { @id = "menuid" })

                                                                </th>
                                                                <th class="heading_style_a" style="text-align:right"><span class="allowedSpan"> Allowed</span></th>
                                                            </tr>
                                                        </table>
                                                    </li>
                                                }
                                            }

                                    </ul>
                                    <div class="clear"></div>
                                </div>
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6" style="padding:0px 10px 10px 0px">
                <div class="drag_to">Drag and Drop section to home screen section</div>
                <div class="inner_section_page_sec">
                    <div class="main_page_area">
                        <div id="container">
                            <div class="column left">
                                <ul class="sortable-list-disallow">

                                       @foreach (var row in Model._SubMenuListModel)
                                        {
                                            if (!row.allowd)
                                            {
                                                <li class="sortable-item loop_content">
                                                    <table id="dragable_grid">
                                                        <tr>
                                                            <th style="color:#000">
                                                                @row.submenuname
                                                                @Html.Hidden("submenuid", @row.submenuid, new { @id = "submenuid" })
                                                                @Html.Hidden("menuid", @row.submenuid, new { @id = "menuid" })
                                                            </th>
                                                            <th class="heading_style_a" style="text-align:right"><span class="allowedSpan">Denied</span></th>
                                                        </tr>
                                                    </table>
                                                </li>
                                            }
                                        }

                                </ul>
                            </div>
                        </div>
                        <div class="clear"></div>
                    </div>
                </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>


}

部分视图

str.contains

事情就是当我搜索记录时,我最初在局部视图中获得所有子菜单项。然后我想要做的是当我在下拉部分视图中选择特定菜单链接时,应仅使用所选菜单的子菜单进行更新

1 个答案:

答案 0 :(得分:1)

您的ajax函数指定返回类型json,但您调用的是返回视图的控制器方法。然后在成功回调中,您将引用不存在的result属性。相反,您需要使用返回的部分视图更新DOM。此外,无需将传递给控制器​​的数据进行字符串化。你的脚本应该是

$("#selectedMenu").change(function () {
  mnid = $(this).val();
  userId = $("#userid").val();
  $.ajax({
    type: 'POST',
    url: '@Url.Action("getsubmenus", "Users")', // don't hard code url's
    data: { userid: userId, menuid: mnid }, // no quotes
    dataType: 'html', // change this
    success: function (data) {
      $(someElement).html(data); // modify selector to suit
    }
  });
});

旁注:您的部分渲染控件具有重复的id属性(无效的html)和没有索引器的重复name属性,因此在提交表单时无法绑定到模型,尽管我不清楚所有这些隐藏输入的重点是什么,因为没有任何东西可以编辑,所以你只需更新完全相同的数据(降低你的应用程序并打开自己过度发布攻击)