返回updated Partial View时未更新部分视图.我错过了什么
本文关键字:视图 错过了 什么 更新部 updated Partial View 返回 | 更新日期: 2023-09-27 18:19:40
我的Html
@using (Html.BeginForm("userrights", "Users", FormMethod.Post, new { @class = "form-horizontal", role = "form", @id = "userrights_frm", @name = "userrights_frm" }))
{
<div class="form-group">
<div class="col-md-7" style="padding-right:10px">
@Html.TextBoxFor(m => m.firstname, new { @class = "form-control", @id = "firstname", @name = "firstname", @placeholder = "Serach By User Name" })
@Html.HiddenFor(m => m.id, new { @class = "form-control", @id = "id", @name = "id" })
</div>
<div class="col-sm-2">
<button type="submit" id="btn_search" value="Search" name="btn_search" class="btn btn-default">Search</button>
</div>
<div class="clearfix"></div>
</div>
<div class="form-group">
<div class="col-md-1" style="padding-top:7px"> Select Top Link </div>
<div class="col-sm-6">
@Html.DropDownList("selectedMenu", Model._menu.Select(menu => new SelectListItem { Text = menu.menuname, Value = menu.menuid.ToString() }), "Select Menu", new { @class = "form-control" })
</div>
<div class="clear"></div>
</div>
@Html.Partial("_userRightPartial", Model._UserRightPartialView)
<div class="clear"></div>
}
选择菜单上的AjaxMethod已更改
$("#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);
}
}
});
});
在Ajax"getsubmenus"中调用的操作结果
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);
}
操作结果当我点击搜索按钮
[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);
}
局部视图
@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>
}
问题是,当我搜索记录时,我最初会得到部分视图中的所有子菜单项。然后我想做的是,当我在下拉菜单中选择特定的菜单链接时,部分视图应该只用所选菜单的子菜单更新
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
属性的控件,但没有索引器,因此在提交表单时将无法绑定到模型,尽管不清楚所有这些隐藏输入的意义是什么,因为没有什么可以编辑的,所以你只会原封不动地发布完全相同的数据(降低你的应用程序的性能,并使自己受到过度发布攻击)