2014-12-01 31 views
1

我有一個強類型的視圖(綁定到userController)列出用戶特定的角色和下面,我有一個下拉列表包含所有提交按鈕的角色。我需要的只是爲該用戶分配新的角色。 ActionResult方法在UserRolesController中。如何將按鈕上的userId和RoleId傳遞給ActionResult方法。傳遞參數在按鈕點擊強類型視圖到另一個控制器

的ActionResult方法UserRolesController:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult AddRole(UserRole userRole, int roleId, int userId) 
    { 
     if (!ModelState.IsValid) return View(userRole); 

     var check = db.UserRoles.Any(x => x.RoleID == roleId && x.UserID == userId); 
     if (check) 
      ViewBag.ResultMessage = "This user already has the role specified !"; 
     else 
      db.UserRoles.Add(userRole); 
     db.SaveChanges(); 
     ViewBag.ResultMessage = "User added to the role succesfully !"; 
     return RedirectToAction("Index"); 
    } 

查看這樣的:

@model IEnumerable<MvcAppCRUD.user> 

@{ 
ViewBag.title = "AssignRole"; 
} 

<h2>Assign Role</h2> 
@if (!Model.Any()) 
{ 
@Html.Label("No Roles assigned for this user") 
} 
else 
{ 
<table> 
    <tr> 
     <th> 
      @Html.DisplayName("Email") 
     </th> 
     <th> 
      @Html.DisplayName("Role Name") 
     </th> 
     <th></th> 
    </tr> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.email) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.RoleName) 
      </td> 
      <td> 
       @Html.ActionLink("Delete", "Delete", new {id = item.id}) 
      </td> 
     </tr> 
    } 
</table> 
} 
<hr /> 
    <div class="display-label"> 
     @Html.DisplayName("Add Role") 
</div> 

<div class="display-field"> 
@Html.DropDownList("Roles", (SelectList) ViewBag.Roles) 
</div> 

@using (Html.BeginForm("AddRole", "UserRoles")) 
{ 
<div class="message-success">@ViewBag.ResultMessage</div> 
} 
<p> 
    <input type="submit" value="Assign" /> 
</p> 
<p> 
@Html.ActionLink("Back to List", "Index") 
</p> 

模型實體:

public partial class UserRole 
{ 
public int ID { get; set; } 
public int UserID { get; set; } 
public int RoleID { get; set; } 
public int Status { get; set; } 

public virtual user Users { get; set; } 
public virtual Role Roles { get; set; } 
} 

public partial class user 
{ 
public user() 
{ 
    Roles = new List<SelectListItem>(); 
} 

public long id { get; set; } 
public string email { get; set; } 
public string password { get; set; } 
public System.DateTime reg_date { get; set; } 
public byte validated { get; set; } 
public virtual ICollection<UserRole> UserRoles { get; set; } 
public int RoleId { get; set; } 
public string RoleName { get; set; } 

public IEnumerable<SelectListItem> Roles { get; set; } 

//public IEnumerable<Role> Roles { get; set; } 
} 

public partial class Role 
{ 
public int ID { get; set; } 
public string RoleName { get; set; } 
public string Desc { get; set; } 
public int Status { get; set; } 
public virtual ICollection<UserRole> UserRoles { get; set; } 
} 

在按鈕點擊沒有任何反應。是否可以將值作爲參數從一個模型視圖傳遞到另一個模型視圖?

+0

您沒有向您展示模型或GET方法。你的模型是否包含一個名爲'Roles'的屬性?您不應該給模型屬性和'ViewBag'屬性使用相同的名稱。 – 2014-12-01 22:29:33

+0

您還將'IEnumerable 傳遞給模型。但期望只傳回一個用戶(哪一個?),並且你沒有控制在你的內部,所以沒有任何事情會回來。 – 2014-12-01 22:34:57

+0

我已經更新了代碼。是的,我已經在用戶模型中聲明瞭Roles屬性。 – naeemmalik 2014-12-01 22:35:55

回答

0

你的代碼有很多問題。特別是您將模型IEnumerable<user>傳遞給不包含或在您的表單中提供任何控件的模型,因此無論如何都不會回發,並且無論如何您都不能回發UserRole,因爲它的複雜對象和下拉列表僅返回單個值。在下拉列表中顯示所有角色沒有意義,然後檢查它是否已在回發中選擇 - 只包括用戶在創建視圖時尚未擁有的那些角色。然後將消息分配給ViewBag,然後重定向是毫無意義的 - 它立即丟失。

創建一個視圖模型來表示要顯示和編輯(注意我已經排除屬性顯示現有角色)

public class UserRoleVM 
{ 
    public int ID { get; set; } // user ID for post back 
    public int Name { get; set; } // user name for display in the view 
    [Display(Name="Select new role")] 
    public int SelectedRole { get; set; } 
    public SelectList RoleList { get; set; } 
} 

控制器什麼

public ActionResult AddRole(int ID) 
{ 
    UserRoleVM model = new UserRoleVM(); 
    var user = // Get the user based on the ID 
    model.ID = ID; 
    model.Name = user.?? 
    var roles = // Get all roles and remove those that the user already has 
    model.RoleList = new SelectList(roles, "ID", "RoleName"); 
    return View(model); 
} 

查看

@model UserRoleVM 
@using(Html.BeginForm()) 
{ 
    <h2>@Model.Name</h2> // users name 
    @Html.LabelFor(m => m.SelectedRole) 
    @Html.DropDownListFor(m => m.SelectedRole, Model.RoleList) 
    <input type="submit" value="Add Role" /> 
} 

Post method

[HttpPost] 
public ActionResult AddRole(UserRoleVM model) 
{ 
    // model is now populated with the ID of the user and the ID of the selected role 
    // save and redirect 
} 
+0

這是非常有用的,使一切都如此清晰,我將在我的應用程序中實現它。你能指導我如何獲得未分配給用戶的角色嗎?還有一件事,我可以在哪裏創建這些Action方法?我應該創建新的控制器還是包含現有的控制器?非常感謝您的幫助。 – naeemmalik 2014-12-02 05:25:22

+0

我不知道你的應用很難說它應該在哪裏。因爲它與'User'相關,所以它可以在'UserController'中,但是您可以再次使用'RolesController'來添加和刪除角色。沒有硬性和快速的規則,你只需要保持一致並保持邏輯,以便其他人可以遵循你的代碼。 – 2014-12-02 05:37:35

+0

至於獲取未分配給用戶的角色,您首先會從數據庫中獲取所有角色,然後刪除已分配給用戶的角色。 [這個答案](http://stackoverflow.com/questions/27224542/passing-response-from-ajax-call-to-view-in-c-sharp/27225961#27225961)給出了一個例子,使用簡單的linq表達式'除了()',但是有很多方法可以做(只是不要像OP那樣做這個問題!)。 – 2014-12-02 05:39:53

相關問題