tidjok tidjok - 2 months ago 16x
C# Question

MVC RadioButtonFor Confusion

I was watching a tutorial video, https://www.youtube.com/watch?v=04RY04uKq2s , and I'm just not understanding what's happening with RadioButtonFor.

Basically, the guy creates a Company class:

public class Company

public string SelectedDepartment {get; set;}

public List<Department> Departments
SampleDBContext db = new SampleDBContext();
return db.Departments.ToList();


Ok, pretty straight-forward. He uses EntityFramework here, which has generated the SampleDBContext class.

Then he has a very simple Index ActionMethod:

public ActionResult Index()
Company company = new Company();
return View();

A strongly-typed View for this [HttpGet]:

@using (Html.BeginForm())
foreach (var department in Model.Departments)
@Html.RadioButtonFor(m => m.SelectedDepartment, department.Id, (department.IsSelected.HasValue && department.IsSelected.Value) ? new (@checked = "checked")} : null

<input type="submit" value="Submit" />[Resulting View][1]

And finally this HttpPost ActionMethod:

public string Index(Company company)
return "You selected nothing";
return "You selected department id: " + company.SelectedDepartment;

Ok, whew. The database has 3 columns, Id, Name and IsSelected (a bit type).


Long post? I hope not. Every piece is very straight-forward.

Now for my questions:

(1)How does RadioButtonFor() use (m=>m.SelectedDepartment)? As in, why does the [HttpPost] method check to see if it's string is null or empty?

(2)When you print out the value of company.SelectedDepartment in the method, you get the ID. Where does this assignment take place?


In your view code there is missing definition of the actual model the view works with. You can do that by adding @model CompanyNamespace.Company.

  1. m => m.SelectedDepartment is a lambda that selects property of the Model which you can check in your Post action to retrieve value of a radio button the user has selected. In case user didn't select any radio button, that value is null (or in general default(T), in this case default(string) = null), thus the if condition in the Post action.

  2. Second argument of RadioButtonFor specifies what value will each radio button have. Since in your case it is department.Id, the value of each generated radio button is Id of a department.

As for the code, as someone else already pointed out in the comment to your answer, I wouldn't really recommend you to use the aproach you posted and probably switch to some other source for learning MVC. IIRC, there are same easy-to-follow tutorials on asp.net.

Edit: When you submit the form, the request is routed to the action decorated with [HttpPost], and model binder binds posted data to the argument (or arguments) of your method by trying to match either argument names or properties of complex object to Post data. So when user selected some radio button it's value (which is taken from department.Id as specified by RadioButtonFor call) is assigned to company.SelectedDepartment property.