2014-01-13 158 views
1

我有一個頁面用於管理UserRoles(將用戶分配給角色),並且我想將我的角色的默認選擇設置下拉爲特定角色,因爲它是最常被用戶添加的,但它不是列表中的第一個角色,這是我開始的。MVC4 SelectList Issue ...爲頁面設置默認選擇下拉頁面

默認情況下,我應該使用什麼語法在角色列表中選擇不同的項目,而不是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> 
} 
+3

wh如果您將SelectedRoleID設置爲您想要選擇的角色,那麼您正在構建視圖模型 –

+0

@MattBodily:來到這裏只是爲了說明這一點。如果可能的話,我會在您的ViewModel構造函數中創建該集合。我試圖儘可能多地拖欠我的拖欠款。 – Ellesedil

+0

謝謝。感到非常愚蠢的錯過了。星期一! –

回答

1

你需要找到一個辦法知道哪一個角色應該是默認,所以無論你是否在Role課程中添加bool IsDefault

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))