Reputation: 331
I am sorry if I ask a newbie question but i am new to asp.net mvc.
So, I have this view :
@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>
}
And the 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>
My question is next : How do I see what radio buttons have been checked in my controller after the submit button has been pressed? I want to have the following logic : If Applicant have been selected then userRole is Applicant , else if Professor has been selected then userrole is Professor. My controller is Empty momentarily because I don't know what to write in it.
Upvotes: 0
Views: 56
Reputation:
If your action method is
public SelectRolesOverall(SelectRolesViewModel model)
then you can access the collection with
IEnumerable<UsersAndRoles> usesAndRoles = model.UsersAndRoles;
and access each item in the collection
foreach (UserRole userRole in model.UsersAndRoles)
{
string role = userRole.Role;
string name = userRole.UserName; // see note below
}
Note you have not included an input for the property UserName
so this value wont post back and you might have trouble matching the role to the user. You might want to add @Html.HiddenFor(m => m.UserName)
or change <td>@Model.User.UserName</td>
to <td>@Html.TextBoxFor(m => m.UserName, new { @readonly = "readonly" })</td>
Upvotes: 1
Reputation: 189
Try this
public ActionResult [YourActionName](string role)
{
switch(role){
case "Applicant": /*your applicant logic*/
break;
case "Professor": /*your Professor logic*/
break;
/*
Other logic here
*/
}
Replace the action name by your own
Note that the parameter role
should have the same name of the radio button in your view, this is to allow data binding to work
Upvotes: 0