I have found couple questions about my problem but none of them actually addresses the problem and gives alternative solutions to problem, thats why I am asking this again.
I am using strongly typed HTML Helpers and no ViewData and ViewBag for only page titles.
Here is my problem.
I have following viewmodel.
public class RegisterViewModel
{
public string Mail { get; set; }
public string Name { get; set; }
public ThreePartDatePickerViewModel ThreePartDateSelection { get; set; }
public RegisterViewModel()
{
ThreePartDateSelection = new ThreePartDatePickerViewModel();
}
}
Above viewmodel uses below viewmodel which basically holds data for 3 dropdownlist which are Day, Month and Year.
public class ThreePartDatePickerViewModel
{
public string Day { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public IList<SelectListItem> Years { get; set; }
public IList<SelectListItem> Months { get; set; }
public IList<SelectListItem> Days { get; set; }
public ThreePartDatePickerViewModel()
{
var now = DateTime.Now;
Years = new List<SelectListItem>();
Months = new List<SelectListItem>();
Days = new List<SelectListItem>();
var empty = new SelectListItem { Text = "" };
Years.Add(empty);
Months.Add(empty);
Days.Add(empty);
foreach(var item in Enumerable.Range(0, 100).Select(x => new SelectListItem { Value = (now.Year - x).ToString(), Text = (now.Year - x).ToString() }))
Years.Add(item);
foreach(var item in Enumerable.Range(1, 12).Select(x => new SelectListItem { Value = x.ToString(), Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x) }))
Months.Add(item);
foreach(var item in Enumerable.Range(1, 31).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() }))
Days.Add(item);
}
}
In the action method which returns RegisterViewModel to view, I am setting the Day,Month and Year properties of ThreePartDatePickerViewModel.
I have checked these values on runtime while views are generated and they are correct.
In my main view,
@model Bla.Bla.RegisterViewModel
<!-- Helpers for other properties -->
@Html.EditorFor(m => m.ThreePartDateSelection)
And my ThreePartDatePickerViewModel Editor Template is
@model Bla.Bla.ThreePartDatePickerViewModel
<div class="threePartDatePicker">
@Html.DropDownListFor(m => m.Day, Model.Days)
@Html.DropDownListFor(m => m.Month, Model.Months)
@Html.DropDownListFor(m => m.Year, Model.Years)
</div>
In my final rendered html, I have all the controls as expected. Dropdowns are rendered fine except the values I have set in action method are not selected.
If I switch from EditorTemplates to Partial Views, It starts working. Or If directly render dropdownlists inside my main view, instead of passing model to EditorFor, It again works.