我有一个用于管理UserRoles的页面(将用户分配给角色),我想将我的角色的默认选择设置为特定角色,因为它是最常被用户添加的角色,但它不是列表中的第一个角色,这是我开始使用的。
默认情况下,我应该使用什么语法来选择角色列表中的其他项而不是Model.Roles.First()。ID?或者这是浪费时间?
型号:
public class User
{
public virtual int ID { get; set; }
public virtual String LanID { get; set; }
public virtual bool IsActive { get; set; }
public virtual Division Division { get; set; }
public virtual Region Region { get; set; }
public virtual Location Location { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
public class Role
{
public virtual int ID { get; set; }
public virtual String Name { get; set; }
public virtual String Description { get; set; }
public virtual bool IsActive { get; set; }
public virtual bool IncludeInReports { get; set; }
}
public class UserRole
{
public virtual int ID { get; set; }
public virtual User User { get; set; }
public virtual Role Role { get; set; }
public virtual bool IsActive { get; set; }
public virtual bool IsOmniscient { get; set; }
public virtual bool IsReadOnly { get; set; }
public virtual System.Nullable<DateTime> StartDateTime { get; set; }
public virtual System.Nullable<DateTime> EndDateTime { get; set; }
}
视图模型:
public class UserRoleViewModel
{
public int ID { get; set; }
public List<User> Users { get; set; }
public List<Role> Roles { get; set; }
[Required]
public int SelectedUserID { get; set; }
[Required]
public int SelectedRoleID { get; set; }
[Required]
public bool IsActive { get; set; }
public bool IsReadOnly { get; set; }
public bool IsOmniscient { get; set; }
[DataType(DataType.Date)]
public System.Nullable<DateTime> StartDateTime { get; set; }
[DataType(DataType.Date)]
public System.Nullable<DateTime> EndDateTime { get; set; }
}
查看:(Create.cshtml)
@model HolterManagementUI.Models.UserRoleViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create a User Role</h2>
@using (Html.BeginForm())
{
<table>
<tr>
<td>Users</td>
<td>
@Html.DropDownListFor(h => h.SelectedUserID, new SelectList(Model.Users, "ID", "Description", Model.Users.First().ID))
</td>
</tr>
<tr>
<td>Roles</td>
<td>
@Html.DropDownListFor(h => h.SelectedRoleID, new SelectList(Model.Roles, "ID", "Description", Model.Roles.First().ID))
</td>
</tr>
<tr>
<td>Read Only</td>
<td>
@Html.HiddenFor(h => h.ID)
@Html.EditorFor(h => h.IsReadOnly)</td>
</tr>
<tr>
<td>Can See Other Areas</td>
<td>
@Html.EditorFor(h => h.IsOmniscient)</td>
</tr>
<tr>
<td>Start Date & Time</td>
<td>@Html.EditorFor(h => h.StartDateTime)</td>
</tr>
<tr>
<td>End Date & Time</td>
<td>@Html.EditorFor(h => h.EndDateTime)</td>
</tr>
</table>
<div class="buttongroup" align="left" style="margin-top: 50px">
<input type="submit" name="submitButton" value="Save" />
<button type="button" onclick="location.href='@Url.Action("List")'">Cancel</button>
</div>
}
答案 0 :(得分:1)
您需要找到一种方法来了解哪个角色应该是默认角色,因此您要向bool IsDefault
课程添加Role
:
public class Role
{
public virtual int ID { get; set; }
public virtual String Name { get; set; }
public virtual String Description { get; set; }
public virtual bool IsActive { get; set; }
public virtual bool IncludeInReports { get; set; }
public virtual bool IsDefault { get; set; }
}
并将您的观点更改为:
@Html.DropDownListFor(h => h.SelectedRoleID, new SelectList(Model.Roles, "ID", "Description", Model.Roles.First(x => x.IsDefault).ID))
修改强>
或者你可以将SelectedRoleID
设置为默认值(如果没有设置 - 例如,如果用户正在编辑它,它可能已经预先设置了)并将其传递给您的视图,并更改您的查看:
@Html.DropDownListFor(h => h.SelectedRoleID, new SelectList(Model.Roles, "ID", "Description", Model.SelectedRoleID))