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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:44:09+00:00 2026-05-23T06:44:09+00:00

I am struggling to get the selected value from the hard-coded dropdownlist in mvc

  • 0

I am struggling to get the selected value from the hard-coded dropdownlist in mvc , below is the code:

View:

<tr><td>No of Helmets</td><td><div class="editor-field">

                <%: Html.DropDownList("helmets", (SelectList)ViewData["size"], "--select--")%>
                </div></td></tr>

                 <tr><td>No of Garages</td><td><div class="editor-field">
                 <%: Html.DropDownList("garages", (SelectList)ViewData["garages"], "--select--")%>

Controller:

// dropdown for helmets

 [HttpPost]
    public ActionResult Create(Event trackday, FormCollection formValues)
    {Product product = new Product();//
        ViewBag.mode = "create";

        // for dropdown track
        ITrackRepository trackResp = new TrackRepository();
        IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
        ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");
       var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["helmets"] = new SelectList(helmets.ToList(), "Value", "Text");

        // dropdown for garages
        var garages = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["garages"] = new SelectList(garages.ToList(), "Value", "Text");  product.QtyAvailable = Convert.ToInt32(formValues["garages"]);


        if (ModelState.IsValid)
        {
            trackday.DateAdded = DateTime.Now;
            trackday.DateModified = DateTime.Now;
          //  productResp.Save();//
         //  trackday.Products.Add(product);
            trackdayResp.Add(trackday);
            trackday.Products.Add(product);
            trackdayResp.Save();
            return RedirectToAction("Index");
        }
        else
        {
            return View();
        }


    }`

How can I get the Selected value of the above 2 dropdownlist in mvc post controller.

  • 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-23T06:44:10+00:00Added an answer on May 23, 2026 at 6:44 am

    You’ve called your dropdowns “helmets” and “garages” if you debug your action and look at the formValues dictionary your should see these two values.

    Alternatively you could change your model to have a “Helmets” and “Garages” property of type int? and the model binder should populate these values.

    Or you could change your action to something like:

    public ActionResult Create(Event trackday, int? helmets, int? garages, FormCollection formValues)
    

    This should be populated with the id’s (selected value) of the drop down list.

    Update

    Here’s my code that gets the values either from the collection or from the passed properties:

    HTML:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
      <h2><%: ViewData["helmetsCollectionValue"]%></h2>
      <h2><%: ViewData["helmetsProperty"]%></h2>
      <h2><%: ViewData["garagesCollectionValue"]%></h2>
      <h2><%: ViewData["garagesProperty"]%></h2>
      <% using (Html.BeginForm()) { %>
      <p>
        <%: Html.DropDownList("helmets", (SelectList)ViewData["size"], "--select--")%>
      </p>
      <p>
        <%: Html.DropDownList("garages", (SelectList)ViewData["garages"], "--select--")%>
      </p>
      <p>
        <input type="submit" value="Submit" />
      </p>
      <% } %>
    </asp:Content>
    

    Controller:

    public ActionResult Index()
    {
        var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["helmets"] = new SelectList(helmets.ToList(), "Value", "Text");
    
        // dropdown for garages
        var garages = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["garages"] = new SelectList(garages.ToList(), "Value", "Text");
    
        return View();
    }
    
    [HttpPost]
    public ActionResult Index(FormCollection collection, int? helmets, int? garages)
    {
        ViewData["helmetsCollectionValue"] = collection["helmets"];
        ViewData["helmetsProperty"] = helmets;
        ViewData["garagesCollectionValue"] = collection["garages"];
        ViewData["garagesProperty"] = garages;
    
        var helmetsList = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["helmets"] = new SelectList(helmetsList.ToList(), "Value", "Text");
    
        // dropdown for garages
        var garagesList = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["garages"] = new SelectList(garagesList.ToList(), "Value", "Text");
    
        return View();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using ASP.NET MVC 3, I have been struggling to set the selected value for
Problem: Been struggling to get my code to load external shaders and it is
I'm struggling to get offline files working in Chrome. The first view of the
I need to get the selected time range from the a mySQL database but
What's wrong with this code ? I have been struggling to get dwb2 ColorFiltered
I was struggling to get int values from a column in a database into
Struggling to get any list function to work. I've been fine with _show and
Attempting/struggling to get registration and sign-up working within an active admin project. I have
I'm really struggling to get my head around Drupal Form API...actually Drupal as a
I am struggling to get a couple of javascripts scripts to work properly. The

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.