Hi,
I am using the DataAnnotations for error checking on my asp.net mvc app, I am also using strongly typed ViewModels too.
My error checking is working fine and is posting back to my view with error messages if a field is blank. However i have a MultiSelect / Listbox on my form which i need to remember it's state after an error.
At the moment my ViewModel looks like this (i have only included relevant properties):
public class ProfilePageViewModel : PageViewModel
{
public IList<FavouriteGenreViewModel> FavGenres { get; set; }
[Required(ErrorMessage = "*")]
public string SelectedGenres { get; set; }
public IDictionary<int, string> GenresList { get; set; }
}
This is my Action in my controller:
public ActionResult Profile(ProfilePageViewModel viewModel)
{
if(!ModelState.IsValid)
{
viewModel.CountriesList = dropDownTasks.GetCountries();
viewModel.GendersList = dropDownTasks.GetGenders();
viewModel.GenresList = dropDownTasks.GetGenres();
viewModel.TimezonesList = dropDownTasks.GetTimezones();
viewModel.FavGenres =
return View(viewModel);
}
. . .
My MultiSelect takes a list of FavouriteGenreViewModel's to select the options in the GenresList, it does this using AutoMapper in GET action, but obviouslly i can't use AutoMapper on the post because it will forget my posted values.
I have thought about using a comma delimmated string of ID's instead of a list of FavouriteGenreViewModel's, that way i can re-use the value once posted back...however i am hoping someone has a more elegant way of dealing with this problem.
Thank you!
Paul