C# 将强类型视图中单击按钮时的参数传递给另一个控制器
我有一个强类型视图(绑定到userController),其中列出了具有特定角色的用户,下面我有一个dropdownlist,其中包含具有提交按钮的所有角色。我所需要的就是给那个用户分配一个新角色。ActionResult方法位于UserRolesController中。如何将按钮单击时的userId和RoleId传递给ActionResult方法 UserRolesController中的ActionResult方法:C# 将强类型视图中单击按钮时的参数传递给另一个控制器,c#,asp.net,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc 4,Razor,我有一个强类型视图(绑定到userController),其中列出了具有特定角色的用户,下面我有一个dropdownlist,其中包含具有提交按钮的所有角色。我所需要的就是给那个用户分配一个新角色。ActionResult方法位于UserRolesController中。如何将按钮单击时的userId和RoleId传递给ActionResult方法 UserRolesController中的ActionResult方法: [HttpPost] [ValidateAntiForgeryToken]
[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>
@model IEnumerable
@{
ViewBag.title=“分配角色”;
}
分配角色
@如果(!Model.Any())
{
@Label(“没有为此用户分配角色”)
}
其他的
{
@Html.DisplayName(“电子邮件”)
@Html.DisplayName(“角色名”)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.email)
@DisplayFor(modelItem=>item.RoleName)
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
}
@Html.DisplayName(“添加角色”)
@Html.DropDownList(“角色”,(选择列表)ViewBag.Roles)
@使用(Html.BeginForm(“AddRole”、“UserRoles”))
{
@ViewBag.ResultMessage
}
@ActionLink(“返回列表”、“索引”)
模型实体:
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; }
}
公共部分类用户角色
{
公共int ID{get;set;}
public int UserID{get;set;}
public int RoleID{get;set;}
公共int状态{get;set;}
公共虚拟用户{get;set;}
公共虚拟角色{get;set;}
}
公共部分类用户
{
公共用户()
{
角色=新列表();
}
公共长id{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
public System.DateTime注册表_date{get;set;}
已验证的公共字节{get;set;}
公共虚拟ICollection用户角色{get;set;}
public int RoleId{get;set;}
公共字符串RoleName{get;set;}
公共IEnumerable角色{get;set;}
//公共IEnumerable角色{get;set;}
}
公共部分类角色
{
公共int ID{get;set;}
公共字符串RoleName{get;set;}
公共字符串Desc{get;set;}
公共int状态{get;set;}
公共虚拟ICollection用户角色{get;set;}
}
按一下按钮,什么也没发生。是否可以将值作为参数从一个模型视图传递到另一个模型视图?您的代码存在许多问题。特别是将
IEnumerable
传递到模型时,表单中不包含或呈现任何控件,因此不会回发任何内容,并且在任何情况下都不能回发UserRole
,因为它是一个复杂的对象,dropdownlist只返回一个值。在下拉列表中显示所有角色,然后在回发时检查是否已选中这些角色是没有意义的——只包括用户在创建视图时尚未拥有的角色。将消息分配给ViewBag
,然后重定向是毫无意义的——它会立即丢失
创建视图模型以表示要显示和编辑的内容(注意,我已排除了用于显示现有角色的属性)
控制器
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);
}
看法
您尚未显示您的模型或GET方法。您的模型是否包含名为
角色的属性?您不应为模型属性和ViewBag
属性指定相同的名称。您还应将`IEnumerable'传递给模型。但是希望只传回一个用户(哪一个?),并且表单中没有控件,因此没有任何回发。我已经更新了代码。是,我已在用户模型中声明了Roles属性。我已将dropdownlist绑定到Roles实体。我可以从中得到roleId。在我的例子中,我需要userId(id)作为参数传递。如何传回从LINQ查询中获得并在视图中列出的值?有什么建议吗?这非常有帮助,而且让一切都变得非常清楚,我将在我的应用程序中实现它。您能指导我如何获得未分配给用户的角色吗。还有一件事,我可以在哪里创建这些动作方法?我应该创建新控制器还是包含在现有控制器中。谢谢,非常感谢你的帮助。我不知道你的应用程序,所以很难说它应该在哪里。由于它与User
相关,因此它可以位于UserController
中,但您也可以使用RolesController
来添加和删除角色。没有硬性规定,您只需要保持一致性并保持逻辑性,以便其他人可以遵循您的代码。至于获取未分配给用户的角色,您将首先从数据库中获取所有角色,然后删除已分配给用户的角色。使用一个简单的LINQ表达式给出一个例子,使用<代码>例外()/<代码>,但是有很多方法可以做(只是不要在那个问题上这样做)。最后我建议您考虑呈现一个复选框列表来显示所有角色。创建视图时,将检查用户已经拥有的角色。管理员可以根据需要选中或取消选中角色,并立即保存所有内容,而不是一次添加和/或删除一个角色(这不是一个好的用户体验)。也许会指引你。在您的情况下,视图模型将具有UserID
和List
,其中RoleVM
具有ID
,Name
和IsSelected
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" />
}
[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
}