Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 552691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:31:36+00:00 2026-05-13T11:31:36+00:00

I am using the DataAnnotations for error checking on my asp.net mvc app, I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:31:37+00:00Added an answer on May 13, 2026 at 11:31 am

    I think i can answer my own question after some poking around.

    In my ViewModel i use a string array as the Data Type like so:

    public class ProfilePageViewModel : PageViewModel 
    { 
    [Required(ErrorMessage = "*")] 
    public string[] FavGenres { get; set; } 
    
    public IDictionary<int, string> GenresList { get; set; } 
    } 
    

    In my view i can set the selected values to FavGenres, then i have a custom model binder to convert the comma seperated string into valid objects again…in case you are wondering here is my custom model binder in full…

    public class AccountCustomModelBinder : DefaultModelBinder
    {
        private readonly IGenreRepository genreRepository;
        private readonly ITimezoneRepository timeZoneRepository;
        private readonly ICountryRepository countryRepository;
    
        public AccountCustomModelBinder() : this(
            ServiceLocator.Current.GetInstance<IGenreRepository>(),
            ServiceLocator.Current.GetInstance<ITimezoneRepository>(),
            ServiceLocator.Current.GetInstance<ICountryRepository>())
        {
        }
    
        public AccountCustomModelBinder(IGenreRepository genreRepository, ITimezoneRepository timeZoneRepository,
            ICountryRepository countryRepository)
        {
            Check.Require(genreRepository != null, "genreRepository is null");
            Check.Require(timeZoneRepository != null, "timeZoneRepository is null");
            Check.Require(countryRepository != null, "countryRepository is null");
    
            this.genreRepository = genreRepository;
            this.timeZoneRepository = timeZoneRepository;
            this.countryRepository = countryRepository;
        }
    
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            Account account = bindingContext.Model as Account;
    
            if (account != null)
            {
    
                // gender
                if (propertyDescriptor.Name == "Gender")
                {
                    if (bindingContext.ValueProvider.ContainsKey("Gender"))
                    {
                        account.Gender = bindingContext.ValueProvider["Gender"].AttemptedValue.ToString();
                        return;
                    }
                }
    
                // TimezoneId
                if (propertyDescriptor.Name == "TimezoneId")
                {
                    if (bindingContext.ValueProvider.ContainsKey("TimezoneId")) {
                        account.Timezone = timeZoneRepository.FindOne(Convert.ToInt32(bindingContext.ValueProvider["TimezoneId"].AttemptedValue));
                        return;
                    }
                }
    
                // CountryId
                if (propertyDescriptor.Name == "CountryId")
                {
                    if (bindingContext.ValueProvider.ContainsKey("CountryId")) {
                        account.Country = countryRepository.FindOne(Convert.ToInt32(bindingContext.ValueProvider["CountryId"].AttemptedValue));
                        return;
                    }
                }
    
                // FavGenres
                if (propertyDescriptor.Name == "FavGenres")
                {
                    if (bindingContext.ValueProvider.ContainsKey("FavGenres")) {
                        // remove all existing entries so we can add our newly selected ones
                        account.ClearFavGenres();
                        string favIds = bindingContext.ValueProvider["FavGenres"].AttemptedValue;
                        foreach (string gId in favIds.Split(',')) {
                            account.AddFavGenre(genreRepository.Get(Convert.ToInt32(gId)));
                        }
                        return;
                    }
                }
            }
    
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 450k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just been having the same issue and I found that:… May 15, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer After exchanging messages with the Godaddy Support Team i found… May 15, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer I suspect it is because you are adding padding to… May 15, 2026 at 8:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.