Asp.net mvc 2 我如何通过从下拉组合中传递固定字符串和值来调用控制器操作?
我确信我想做的是可能的,但我不知道怎么做 我有一个视图,其中显示了有关所选用户的一些信息,包括他们的角色。我在视图中添加了一个显示所有角色的下拉列表,并希望有一个按钮,可以将下拉列表中选定的角色添加到当前用户。要允许此操作,我有一个使用此方法的控制器:Asp.net mvc 2 我如何通过从下拉组合中传递固定字符串和值来调用控制器操作?,asp.net-mvc-2,post,Asp.net Mvc 2,Post,我确信我想做的是可能的,但我不知道怎么做 我有一个视图,其中显示了有关所选用户的一些信息,包括他们的角色。我在视图中添加了一个显示所有角色的下拉列表,并希望有一个按钮,可以将下拉列表中选定的角色添加到当前用户。要允许此操作,我有一个使用此方法的控制器: public ActionResult AddUserRole (string userName,string roleName) { if (Roles.IsUserInRole (userName,roleName)==fal
public ActionResult AddUserRole (string userName,string roleName)
{
if (Roles.IsUserInRole (userName,roleName)==false)
{
Roles.AddUserToRole (userName,roleName);
}
return RedirectToAction("Profile", "Profile",new {userName=userName});
}
但是我不知道如何将视图下拉列表中的选定项设置为controller方法中的string roleName
参数。我可以很容易地设置用户名,因为这是静态的。我错过了什么?以下是我的观点,或者至少是相关的观点:
<%
using (Html.BeginForm( "AddUserRole", "Account",new {userName=Model.UserName}))
{%>
<div id="AddRoleToUser">
<asp:Label ID="Label1" runat="server" Text="Select new role."></asp:Label>
<%:Html.DropDownListFor(model=>model.Roles,new SelectList (Model.Roles),null,new {id="roleName"}) %>
<input type="submit" value="Create" />
</div>
<% }%>
<%}%>
model.Roles,新选择列表(model.Roles),null,新{id=“roleName”})%>
Model.Roles是一种
IEnumerable
类型 在控制器操作中更改参数的名称似乎满足了我的要求:
public ActionResult AddUserRole (string userName,string roles)
{
if (Roles.IsUserInRole (userName,roles)==false)
{
Roles.AddUserToRole (userName,roles);
}
return RedirectToAction("Profile", "Profile",new {userName=userName});
}
<%
using (Html.BeginForm( "AddUserRole", "Account",new {userName=Model.UserName}))
{%>
<div id="AddRoleToUser">
<asp:Label ID="Label1" runat="server" Text="Select new role."></asp:Label>
<%:Html.DropDownListFor(model=>model.Roles,new SelectList (Model.Roles)) %>
<input type="submit" value="Create" />
</div>
<% }%>
<%}%>
public ActionResult AddUserRole(字符串用户名,字符串角色)
{
if(Roles.IsUserInRole(userName,Roles)==false)
{
Roles.AddUserToRole(用户名、角色);
}
返回RedirectToAction(“Profile”,“Profile”,new{userName=userName});
}
model.Roles,新选择列表(model.Roles))%>
我不知道我将如何将其别名为其他内容,但现在它可以工作,所以不会有问题