1,在给一个layer弹出层做checkbox自动选中时,ViewBag传递的时object类型,用不了lambda表达式,代码如下,
@foreach (var perm in ViewBag.allPerms) { <input type="checkbox" id="permissionIds_@perm.Id" name="PermissionIds" value="@perm.Id" checked="@(ViewBag.RolePerms.Select(r=>r.Id).Contains(perm.Id))"/> <label for="permissionIds_@perm.Id">@perm.Name</label> }
2,于是在弹出层就不写foreach循环遍历checkbox,直接在点击编辑事件时的弹出层中 ‘添加层弹出后的成功回调方法success' ,在里面通过ajax填充数据
Control中Edit方法获取数据
//填充角色数据并显示 public ActionResult Edit(long roleId = 0) { if (roleId == 0) { return Json(new JsonData { State = 0, ErrorMsg = "角色不存在!" }); } var role = RoleService.GetById(roleId); if (role==null) { return Json(new JsonData { State = 0, ErrorMsg = "角色Id不存在!" }); } var allPerms = PermissionService.GetAll(); var rolePerms = PermissionService.GetByRoleId(roleId); RoleEditGetModel model = new RoleEditGetModel(); model.RolePerms = rolePerms; model.AllPerms = allPerms; return Json(new JsonData { State = 1, Data = model }); }
View中Layer弹出层success方法
success: function (layero, index) { //填充数据 $.ajax({ url: "/Role/Edit?roleId=" + id, type: "post", dataType: "json", success: function (res) { if (res.State == 1) { var html = ""; var bool = false; for (var i = 0; i < res.Data.AllPerms.length; i++) { bool = false; for (var j = 0; j < res.Data.RolePerms.length; j++) { if (res.Data.RolePerms[j].Id == res.Data.AllPerms[i].Id) { bool = true; break; } } html += ' <input ' + (bool ? "checked" : "") + ' type="checkbox" id="permissionIds_' + res.Data.AllPerms[i].Id + '" name="PermissionIds" value="' + res.Data.AllPerms[i].Id + '" /> <label for="permissionIds_' + res.Data.AllPerms[i].Id + '">' + res.Data.AllPerms[i].Name + '</label>' } $("#formValue").html(html); } }, error: function () { layer.alert('网络请求失败!', { title: '提示框', icon: 0, }); } }); },
总结
以上所述是小编给大家介绍的在layer弹出层中通过ajax返回html拼接字符串填充数据,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对阿兔在线工具网站的支持!