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

The Archive Base Latest Questions

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

I’m about to start a new ASP.NET MVC application. In it we have a

  • 0

I’m about to start a new ASP.NET MVC application. In it we have a number of drop downs lists/boxes. I have no problem rending them to the ui etc.

My two questions are:-

  1. Is it possible to bind the selected value of a drop down list to an enumeration? Does anyone else actually do this (if this is possible)
  2. Are there any good code practices to prevent the code from throwning an exception if a user tries to inject a different value for the drop down. Eg. instead of posting the selected value which is a number/int .. they try and hack the post data and change it to a string of non numbers. And what is this security hack/exploit, called?

cheers 🙂

  • 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-13T16:09:37+00:00Added an answer on May 13, 2026 at 4:09 pm

    As jfar posted, use:

    string selectedValue = "1";
    
    SelectListItem[] selectListItems = Enum.GetNames(typeof(MyEnumeration)).Select(
            s => new SelectListItem { Text = s, Value = s, Selected = s == selectedValue}).ToArray();
    

    which is from MVCContrib, you don’t to include the DLL, this is just code found in MVCContrib.

    To protect against CSRF(Cross Site Request Forgery), you can use the <%= Html.AntiForgeryToken() %> in the view under the respective form that will be posted, and decorate the appropriate action with [ValidateAntiForgeryToken]. More details on the Html.AntiForgeryToken() can be found here.

    EDIT As per Comment

    Well first, you’ll need to place the SelectListItem[] in the ViewData so you can access it in the view:

    Action

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult MyView(string enumValue)
    {
        string selectedValue = "1"; // fill this with the value you want to be selected
    
        SelectListItem[] selectListItems = Enum.GetNames(typeof(MyEnumeration)).Select(
                s => new SelectListItem { Text = s, Value = s, Selected = s == selectedValue}).ToArray();
    
        ViewData["enumValue"] = selectListItems;
    
        return View();    
    }
    

    and in your view the following form will work.

    <form method="post">
        <%= Html.AntiForgeryToken() %>
        <%= Html.DropDownList("enumValue") %>
    </form>
    

    The HTML helper will output the proper select control.

    Back in your controller, this is the action that will accept the form post

    [ValidateAntiForgeryToken]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult MyView(int enumValue)
    {
        // enumValue will have the selected value
        ViewData["Message"] = "You selected the Enum name" + Enum.GetName(typeof(MyEnumeration), enumValue);
    
        return View();    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.