一旦用户选择了值并通过视图提交了值,我就无法从控制器中访问用户的下拉选项。
下拉菜单正确填充,我编写了一个简单的JavaScript函数,在进行选择更改时提醒用户。通过警报,我看到了RoleID值。我似乎无法在控制器中访问它。请参阅下面的代码并提供帮助!提前谢谢。
型号:
public class dbModels
public class UserAccessRequestViewModel
{
dbEntities _dbEntities = new dbEntities();
public IEnumerable<ActiveRoles> Roles { get; set}
public Guid selectedRoleID { get; set:}
public class ActiveRoles
{
public Guid RoleID { get; set; }
public string RoleName { get; set; }
}
public List<ActiveRoles> GetActiveRoles()
{
var ActiveRoles = dbEntities.LU_ROLE.Where(x => x.ACTIVE)
.Select (x => new ActiveRoles { RoleID = x.ROLE_ID, RoleName = x.ROLE_NAME }).ToArray();
return activeRoles.ToList();
}
}
查看:
<div class="text-left" style="margin:4px; padding:4px">
<h2>Assign Role</h2>
@Html.DropDownList(model => model.selectedRoleID, new SelectList(Model.Roles, "RoleID", "RoleName"), new { @onchange = getUserRoleID(this)" })
</div>
<div class="form-actions no-color" style="margin:4px; padding:4px">
@using (Html.BeginForm("Approve", "UserAccessRequestManagement", FormMethod.Post))
{
<input type="submit" value="Approve" class="btn btn-default" />
}
@Html.ActionLink("Back to List", "Index")
</div>
控制:
public class UserAccessRequestManagementController : Controller
dbModels.UserAccessRequestViewModel _UserAccessRequestViewModel = new dbModels.UserAccessRequestViewModel();
[HttpPost, ActionName("Approve")]
public ActionResult Approve(Guid id)
{
Guid RoleID = _UserAccessRequestViewModel.selectedRoleID;
}
答案 0 :(得分:0)
您的选择输入应该是表单的一部分。
<div class="form-actions no-color" style="margin:4px; padding:4px">
@using (Html.BeginForm("Approve", "UserAccessRequestManagement", FormMethod.Post))
{
<div class="text-left" style="margin:4px; padding:4px">
<h2>Assign Role</h2>
@Html.DropDownList(model => model.selectedRoleID, new SelectList(Model.Roles, "RoleID", "RoleName"), new { @onchange = getUserRoleID(this)" })
</div>
<input type="submit" value="Approve" class="btn btn-default" />
}
@Html.ActionLink("Back to List", "Index")
</div>
此外,您必须确保您希望在Controller操作中获得什么作为参数。 如果您只需要一个guid,那么将参数名称更改为selectedRoleID,因为input元素的名称应与您的action参数匹配。如果您想将整个模型作为参数,那么您必须在行动定义中正确提及它。
如果您想在POST操作中获取整个模型
public class UserAccessRequestManagementController : Controller
dbModels.UserAccessRequestViewModel _UserAccessRequestViewModel = new dbModels.UserAccessRequestViewModel();
[HttpPost, ActionName("Approve")]
public ActionResult Approve(UserAccessRequestViewModel model)
{
Guid RoleID = model.selectedRoleID;
}
或者像这样做如果你只想在你的POST动作中选择一个guid
[HttpPost, ActionName("Approve")]
public ActionResult Approve(Guid selectedRoleID)
{
Guid RoleID = selectedRoleID
}