![](/img/trans.png)
[英]How can I check selected radiobuttons when the radiobuttons have been added dynamically
[英]How do I see in my controller what radiobuttons have been checked?
如果我问一个新手问题,很抱歉,但是我是asp.net mvc的新手。
所以,我有这种看法:
@model FirstProject.Models.SelectRolesViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("SelectRolesOverall","SelectRoles"))
{
<table class="table">
<tr>
<th>Users</th>
<th>Roles</th>
</tr>
@Html.EditorFor(model=>model.UsersAndRoles)
<tr>
<td></td>
<td>
<input type="submit" />
</td>
</tr>
</table>
}
还有EditorTemplate:
@model FirstProject.Models.UserRole
<tr>
<td>@Model.User.UserName</td>
<td>
@Html.RadioButtonFor(model => model.Role, "Applicant") Applicant
<br/>
@Html.RadioButtonFor(model => model.Role, "Professor") Professor
</td>
</tr>
接下来的问题是:按下提交按钮后,如何查看控制器中已选中哪些单选按钮? 我希望具有以下逻辑:如果已选择“申请人”,则userRole为“申请人”,否则,如果已选择“教授”,则userrole为“教授”。 我的控制器暂时为空,因为我不知道该写什么。
如果您的操作方法是
public SelectRolesOverall(SelectRolesViewModel model)
然后您可以使用
IEnumerable<UsersAndRoles> usesAndRoles = model.UsersAndRoles;
并访问集合中的每个项目
foreach (UserRole userRole in model.UsersAndRoles)
{
string role = userRole.Role;
string name = userRole.UserName; // see note below
}
请注意,您尚未包括属性UserName
的输入,因此该值不会回发,因此您可能无法将角色与用户进行匹配。 您可能需要添加@Html.HiddenFor(m => m.UserName)
或将<td>@Model.User.UserName</td>
更改为<td>@Html.TextBoxFor(m => m.UserName, new { @readonly = "readonly" })</td>
尝试这个
public ActionResult [YourActionName](string role)
{
switch(role){
case "Applicant": /*your applicant logic*/
break;
case "Professor": /*your Professor logic*/
break;
/*
Other logic here
*/
}
用自己的名称替换操作名称
请注意 ,参数role
在视图中应具有与单选按钮相同的名称,这是为了允许数据绑定起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.