SelectListItem not Returning Results - MVC5
本文关键字:MVC5 Results Returning not SelectListItem | 更新日期: 2023-09-27 18:20:31
我尝试了许多不同的帖子、标签和混合物,但在我的选择列表中没有返回数据。我甚至已经准备好了,有些人在发送列表之前必须使用jquery来突出显示列表;如果选择的项目实际上通过了项目,那么这是非常可行的!我愿意使用Jquery,我愿意更新模型,我愿意尽一切努力将模型数据返回,但似乎所有这些返回计数都为0或null。
我使用了两个列表框,其中有一些移动,并希望在完成后返回两个列表盒的内容。
(如果还有其他方法可以做到这一点,请分享!)
型号:
public class RoleAddRemoveListBoxViewModel
{
public List<System.Web.Mvc.SelectListItem> CurrentRoles { get; set; }
public List<System.Web.Mvc.SelectListItem> NonMemberRoles { get; set; }
}
控制器:
[HttpPost]
public ActionResult ManageUserRoles(string userList)
{
List<SelectListItem> lbMemberRoles = new List<SelectListItem>();
List<SelectListItem> lbNonMemberRoles = new List<SelectListItem>();
var user = (from u in db.Users
where u.UserName == userList
select u).SingleOrDefault();
// prepopulate roles for the view dropdown
var roleList = db.Roles.OrderBy(r => r.Name).ToList().Select(rr =>
new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
var userRoles = UserManager.GetRoles(user.Id);
foreach (var role in roleList)
{
if (userRoles.Contains(role.Value.ToString()))
{
lbMemberRoles.Add(role);
}
else
{
lbNonMemberRoles.Add(role);
}
}
RoleAddRemoveListBoxViewModel lbvm = new RoleAddRemoveListBoxViewModel
{
CurrentRoles = lbMemberRoles,
NonMemberRoles = lbNonMemberRoles
};
return View(lbvm);
}
[HttpPost]
public ActionResult UpdateUserRoles(RoleAddRemoveListBoxViewModel model)
{
TempData["updateState"] = "User Role Updated Successfully!";
return RedirectToAction("Index");
}
视图:
@model WebDocEditor.Models.RoleAddRemoveListBoxViewModel
@{
using (Html.BeginForm("UpdateUserRoles", "Roles", FormMethod.Post))
{
<body>
<fieldset>
<form>
<div class="row">
<div class="col-xs-2">
@Html.LabelFor(model => model.CurrentRoles)
<div>
@Html.ListBoxFor(model => model.CurrentRoles, Model.CurrentRoles)
</div>
</div>
<div class="btn-group-vertical col-xs-1">
<div id="moveRightbtn" class="btn glyphicon glyphicon-arrow-right"></div>
<div id="moveLeftbtn" class="btn glyphicon glyphicon-arrow-left"></div>
</div>
<div class="col-xs-2">
@Html.LabelFor(model => model.NonMemberRoles)
<div>
@Html.ListBoxFor(model => model.NonMemberRoles, Model.NonMemberRoles)
</div>
</div>
</div>
<input type="submit" value="Save" />
</form>
</fieldset>
</body>
}
}
动作编辑后:我想加入我用于高亮显示的JQuery,因为我看到了很多关于他们如何使其工作的线程,并想展示我的组合/设置的工作原理:JS on View:
@section Scripts
{
<script type="text/javascript">
$(document).ready(function () {
$('#moveRightbtn').on('click', function () {
var selectedCurrent = $('#CurrentRoles option:selected');
$('#NonMemberRoles').append($(selectedCurrent).clone());
$(selectedCurrent).remove()
//alert(selectedCurrent);
})
$('#moveLeftbtn').on('click', function () {
var selectedNonMember = $('#NonMemberRoles option:selected');
$('#CurrentRoles').append($(selectedNonMember).clone());
$(selectedNonMember).remove()
//alert(selectedNonMember);
})
$("#submitBtn").on('click', function (e) {
$("#CurrentRoles option").prop('selected', 'selected');
$("#NonMemberRoles option").prop('selected', 'selected');
});
});
</script>
}
您的列表需要与返回的内容分开。。你应该这样设置你的模型。
public class RoleAddRemoveListBoxViewModel
{
public List<System.Web.Mvc.SelectListItem> CurrentRolesList { get; set; }
public List<System.Web.Mvc.SelectListItem> NonMemberRolesList { get; set; }
public string[] CurrentRoles {get;set;}
public string[] NonMemberRoles {get;set;}
}
列表框将返回所选内容的数组。。在这种情况下,您可以将属性设置为string[]
在您的视图中,使用新属性作为表达式,使用List属性作为selectList
@using (Html.BeginForm())
{
<div class="row">
<div class="col-xs-2">
@Html.LabelFor(model => model.CurrentRoles)
<div>
@Html.ListBoxFor(model => model.CurrentRoles, Model.CurrentRolesList)
</div>
</div>
<div class="btn-group-vertical col-xs-1">
<div id="moveRightbtn" class="btn glyphicon glyphicon-arrow-right"></div>
<div id="moveLeftbtn" class="btn glyphicon glyphicon-arrow-left"></div>
</div>
<div class="col-xs-2">
@Html.LabelFor(model => model.NonMemberRoles)
<div>
@Html.ListBoxFor(model => model.NonMemberRoles, Model.NonMemberRolesList)
</div>
</div>
</div>
<input type="submit" value="Save" />
}
在您的控制器中。。确保您为您的列表设置了正确的属性
public ActionResult ManageUserRoles(string userList)
{
List<SelectListItem> lbMemberRoles = new List<SelectListItem>();
List<SelectListItem> lbNonMemberRoles = new List<SelectListItem>();
var user = (from u in db.Users
where u.UserName == userList
select u).SingleOrDefault();
// prepopulate roles for the view dropdown
var roleList = db.Roles.OrderBy(r => r.Name).ToList().Select(rr =>
new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
var userRoles = UserManager.GetRoles(user.Id);
foreach (var role in roleList)
{
if (userRoles.Contains(role.Value.ToString()))
{
lbMemberRoles.Add(role);
}
else
{
lbNonMemberRoles.Add(role);
}
}
RoleAddRemoveListBoxViewModel lbvm = new RoleAddRemoveListBoxViewModel
{
CurrentRolesList = lbMemberRoles,
NonMemberRolesList = lbNonMemberRoles
};
return View(lbvm);
}
不确定为什么您将此操作结果作为[HttpPost],但您可能希望删除该属性。。
按照列表框的工作方式,它只会发回实际选择的内容。。因此,如果您想返回每个列表中的所有值,则可能必须选择其中的所有项
为返回属性添加string[]
。
public List<System.Web.Mvc.SelectListItem> CurrentRoles { get; set; }
public List<System.Web.Mvc.SelectListItem> NonMemberRoles { get; set; }
public string[] SelectedCurrentRoles{ get; set; }
public string[] SelectedNonMemberRoles{ get; set; }
并在ListBoxFor
中使用MultiSelectList
。我会使用不同的型号通过,但你可以使用现有的List<SelectListItem>
@Html.ListBoxFor(model => model.SelectedCurrentRoles, new MultiSelectList(Model.CurrentRoles, "Value", "Text"))
@Html.ListBoxFor(model => model.SelectedNonMemberRoles, new MultiSelectList(Model.NonMemberRoles , "Value", "Text"))