使用multiSelectList选择列表显示,但未选择所选项目。
在Deckged代码中,在调用ListBox()时,multiSelectList成员,selectedMembers被正确填充。帮助会很棒!
的 更新
从"成员"更改列表框的名称其他任何东西使这项工作......即@ Html.ListBox(" AnythingExceptmembers",Model.members)
模型
public class GroupProfileViewModel
{
//primary key
public int Id { get; set; }
[StringLength(55, MinimumLength = 3)]
[Display(Name = "Group Name")]
public string name { get; set; }
[StringLength(55, MinimumLength = 3)]
[Display(Name = "Group Description")]
[DataType(DataType.MultilineText)]
public string description { get; set; }
[Display(Name = "Date Group Created")]
public DateTime dateTimeCreated { get; set; }
public virtual UserAccountModel creator { get; set; }
public MultiSelectList members { get; set; }
public IEnumerable<GroupModel> groups { get; set; }
}
控制器
public ActionResult GetGroupProfile(string groupId)
{
GroupProfileViewModel model;
...
model.members = new MultiSelectList(db.UserAccounts.ToList(), "Id", "fullName", groupProfile.members.Select(p => p.Id).ToArray());
return PartialView("_groupProfilePartial", model);
}
部分视图
@model MiTasks.Models.GroupProfileViewModel
<form id="frm_groupProfileForm" method="post" action="/Groups/UpdateGroupProfile">
...
<div class="row">
<div class="col-md-12" style="margin-top:3em;">
@Html.ListBox("members", Model.members)
</div>
</div>
</form>
部分查看结果
<div class="row">
<div class="col-md-12" style="margin-top:3em;">
<select id="members" multiple="multiple" name="members">
<option value="1">user1Fname</option>
<option value="2">user2Fname</option>
</select>
</div>
</div>
显示成员的调试图片