I have created a View Model called CompetitionRoundModel
which is partially produced below:
public class CompetitionRoundModel
{
public IEnumerable<SelectListItem> CategoryValues
{
get
{
return Enumerable
.Range(0, Categories.Count())
.Select(x => new SelectListItem
{
Value = Categories.ElementAt(x).Id.ToString(),
Text = Categories.ElementAt(x).Name
});
}
}
[Display(Name = "Category")]
public int CategoryId { get; set; }
public IEnumerable<Category> Categories { get; set; }
// Other parameters
}
I have structured the model this way because I need to populate a dropdown based on the value stored in CategoryValues. So for my view I have:
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CategoryId, Model.CategoryValues, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
// Other code goes here
}
I have selected model.CategoryId
in the DropDownListFor()
method since I want to bind the selected value to CategoryId
. I really don't care for CategoryValues
, I just need it to populate the DropDown.
My problem now is that when my Controller receives the values for my Model in the action method, CategoryValues
is null which causes the system to throw a ArgumentNullException
(the line that is highlighted is the return Enumerable
line.
I have even tried [Bind(Exclude="CategoryValues")]
but no change at all. Any help would be much appreciated.