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 8643705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:07:00+00:00 2026-06-12T12:07:00+00:00

On my MVC form, I need to bind a drop down box to an

  • 0

On my MVC form, I need to bind a drop down box to an enumeration on my ViewModel. The best way I found to do that is described here.

It appeared to work at first, but now that I’ve added validation to my form, I’ve discovered it does not bind back to the ViewModel.

Here’s my razor code:

<div class="editor-field">
    @Html.DropDownListFor(model => model.response, 
                          new SelectList(Enum.GetValues(typeof(Vouchers.Models.ResponseType))),
                          "Please Select")
</div>

And here’s my view model definition for the field:

[DisplayName("Response")]
[Range(1, int.MaxValue, ErrorMessage = "You must select a response before submitting this form.")]
public ResponseType response { get; set; }

The problem is that I cannot submit the form; even after selecting a response from my drop down, the Validation error message for the Range attribute is displayed and the client side validation prevents the form from submitting.

I believe this is because the SelectList for the drop down contains only the string names of the enum, not the underlying integer value.

How can I solve this problem?

  • 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-06-12T12:07:01+00:00Added an answer on June 12, 2026 at 12:07 pm

    Create Dictionary where Key will be integer representation of the enum and string- the name of enum.

    @Html.DropDownListFor(model => model.response, 
                          new SelectList(Enum.GetValues(typeof(Vouchers.Models.ResponseType)).OfType<Vouchers.Models.VoucherResponseType>().ToDictionary(x => Convert.ToInt32(x), y => y.ToString()), "Key", "Value"), "Please Select")
    

    Sorry for possible errors, I have not tried it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple MVC 3 forms that need form validation on certain fields (mostly
Which is the right MVC way to create custom form control (like custom drop
I have a ASP.Net MVC form that upon submit I need a couple of
I have two radio buttons on my MVC form that I use to hide
I have an ASP.NET MVC form that when submitted can return either an ActionResult
I have an ASP.NET MVC 2 form that is working perfectly, doing client side
I just created my first MVC webapp and noticed that I need to store
I need some RadioButtons that grouped together in one of view page at mvc
I need a form on my ASP.NET MVC Razor page. My preference would be
I am working on MVC 4.0. I need to access my dropdown box Text

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.