我已经能够像这样创建viewmodel(请忽略它包含另一个viewmodel,在我解决当前问题后将修复:)):
public class UserViewModel
{
#region Variables
private SecUserViewModel user;
private string[] assignedRolesIds;
private List<SecRoleViewModel> availableRoles;
#endregion
#region Properties
public SecUserViewModel User
{
get { return this.user; }
set { this.user = value; }
}
public string Guid
{
get { return this.user.Guid.ToString(); }
set { this.user.Guid = value; }
}
public string UserName
{
get { return this.user.UserName; }
set { this.user.UserName = value; }
}
public string Email
{
get { return this.user.Email; }
set { this.user.Email = value; }
}
public byte[] AuthDigest
{
get { return this.user.AuthDigest; }
set { this.user.AuthDigest = value; }
}
public bool IsUsingTempPasswd
{
get { return this.user.IsUsingTempPasswd; }
set { this.user.IsUsingTempPasswd = value; }
}
public DateTime? LastLogin
{
get { return this.user.LastLogin; }
set { this.user.LastLogin = value; }
}
public DateTime? PasswordChanged
{
get { return this.user.PasswordChanged; }
set { this.user.PasswordChanged = value; }
}
public string[] AssignedRolesIds
{
get { return this.assignedRolesIds; }
set { this.assignedRolesIds = value; }
}
public List<SecRoleViewModel> AvailableRoles
{
get { return this.availableRoles; }
set { this.availableRoles = value; }
}
#endregion
#region Constructor
public UserViewModel()
{
User = new SecUserViewModel();
AssignedRolesIds = null;
AvailableRoles = new List<SecRoleViewModel>(0);
}
public UserViewModel(SecUserViewModel secUser, List<SecRoleViewModel> roleList, List<SecRoleViewModel> availableList)
{
User = secUser;
AssignedRolesIds = roleList.Select(r => r.Role.Guid.ToString()).ToArray();
AvailableRoles = availableList;
}
#endregion
}
我的控制器有一个编辑操作。在“GET”上,我传递viewmodel并正确显示,包括多选列表和预选值。但是当我“POST”“编辑”时,传回的UserViewModel将“AssignedRolesIds”和“AvailableRoles”清空,尽管其他所有内容都已填满。当我检查FormCollection对象时,会出现“AssignedRolesIds”作为键。
我的观点如下:
@model DocuLive.ViewModels.UserViewModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_AdminPage.cshtml";
}
<h2>Edit</h2>
<div class="error-message">@TempData["Fail"]</div>
<div class="success-message">@TempData["Success"]</div>
@using (Html.BeginForm("Edit", "User", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>SecUser</legend>
<div class="editor-label">
@Html.LabelFor(model => model.User.Guid)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.User.Guid)
@Html.HiddenFor(model => model.User.Guid)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User.UserName)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.User.UserName)
@Html.HiddenFor(model => model.User.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User.Email)
@Html.ValidationMessageFor(model => model.User.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User.IsUsingTempPasswd)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User.IsUsingTempPasswd)
@Html.ValidationMessageFor(model => model.User.IsUsingTempPasswd)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User.LastLogin)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.User.LastLogin)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User.PasswordChanged)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.User.PasswordChanged)
</div>
<div class="hidden-field">
@Html.HiddenFor(model => model.User.AuthDigest)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AssignedRolesIds)
</div>
<div class="editor-field">
@Html.ListBoxFor(x => x.AssignedRolesIds, new MultiSelectList(Model.AvailableRoles, "Guid", "RoleName"), new { Multiple = "multiple", @class = "multi-select-list" })
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Users")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
有人可以建议为什么只传递与ListBoxFor相关的值(有空值)?
提前致谢
答案 0 :(得分:0)
似乎问题是将字符串数组值绑定到复杂对象的字符串id值(在我的情况下是Role) - 但我最终找到了解决它的方法。诀窍是拥有该方法的签名:
[HttpPost]
public ActionResult Edit(UserViewModel user, string [] assignedRolesIds)
然后你看到了这个:
@Html.ListBox("AssignedRolesIds",new MultiSelectList(Model.AvailableRoles, "Guid", "RoleName"),new { Multiple = "multiple", @class = "multi-select-list"})
使用此解决方案,您必须将“assignedRolesIds”重新分配给UserViewModel上的“AssignedRolesIds”属性,但这只是两行(包括检查数组是不是emppty还是null)。