I need to display month and year list to dropdownlist in MVC but i encounter an error "Object reference not set to an instance of an object."
MODEL
public class AccountingViewModel
{
public string StartMonth { get; set; }
public IEnumerable<SelectListItem> Months
{
get
{
return DateTimeFormatInfo
.InvariantInfo
.MonthNames
.Select((monthName, index) => new SelectListItem
{
Value = (index + 1).ToString(),
Text = monthName
});
}
}
}
VIEW
@model TrackAndTrace.ViewModel.AccountingViewModel
@using (Html.BeginForm("AccountingReport", "Report", FormMethod.Post))
{
<div class="form-group">
@Html.LabelFor(model => model.StartMonth)
@Html.DropDownListFor(model=>model.StartMonth,Model.Years)
@Html.ValidationMessageFor(model => model.StartMonth)
</div>
}