I'm just starting to learn MVC4 and I must admit I'm a little challenged by all the reliance on lambda expressions, generics and anonymous types, especially when there are many overloaded functions. It's a bit confusing to try and understand how the HTML Helpers work. Take the "DropDownListFor" Helper, for example. I tried to extract the essentials from my code. That's why you see only a only a single column, "Gender", in the table. Hopefully it is still syntactically correct:
@model IEnumerable<BusinessLayer.Employee>
<table>
@foreach (var employee in Model)
{
<tr>
<td>
@{
var listItems = new List<SelectListItem>()
{
new SelectListItem {Text = "Male", Value = "M"},
new SelectListItem {Text = "Female", Value = "F"}
};
@Html.DropDownListFor(m => employee.Gender, listItems, listItems.Find(i => i.Value == employee.Gender).Text)
}
</td>
</tr>
}
</table>
There are two Employees in the Model object, an IEnumerable of Employee and the following HTML is generated
<table>
<tr>
<td>
<select id="employee_Gender" name="employee.Gender"><option value="">Male</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</tr>
</td>
<select id="employee_Gender" name="employee.Gender"><option value="">Female</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</td>
</table>
Looking at the first param of the DropDownListFor Method,
@Html.DropDownListFor(m => employee.Gender, ...
...Do I understand correctly that the "m" represents the instance of the model class that was passed into the View? In the online examples that I have seen, I have seen examples where the "m" appears to refer to the model class but where the model is a single instance class, for example, a single Employee object, rather than a collection of Employees as is the case here where I want to display a table list of Employees. I'm assuming that the intent of the first param of the DropDownListFor Method is "point/bind' to a property in the model, so that's why I made m => employee.Gender. Is this what I should be doing when the model is a collection of Employees rather than a single Employee? If so, I am confused as to how I can generate unique IDs for the two drop down lists which were both assigned "employee_Gender" for both the NAME and ID attributes.
For the 3rd parameter of the Helper DropDownListFor,
listItems.Find(i => i.Value == employee.Gender).Text
Is this reasonable? The value for employee.Gender is either "M" or "F" but the display Text is "Male" and "Female." I am confused whether this param is used to specify a default "NO SELECT" first value or whether this param is used to select from the drop down the value of the current Employee---or perhaps it can do both? For example, does it select the Text specified if found and otherwise add the text specified as the FIRST? parameter if the value is not found? That appears to be how it works from what I see. Can this 3rd param be simplified or is my example reasonable?
Thanks for your patience in reading my many flurry of questions in trying to better understand this.