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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:46:59+00:00 2026-05-31T20:46:59+00:00

Migrating from textboxe to dropdownlist – Need to send value from a hard-coded dropdownlist

  • 0

Migrating from textboxe to dropdownlist – Need to send value from a hard-coded dropdownlist to controller

The code below is used in the controller

var list = new SelectList(new[]
                                      {
                                          new{ID="1",Name="20012"},
                                          new{ID="2",Name="20011"},
                                          new{ID="3",Name="20010"},
                                          new {ID="4",Name="2009"},
                                          new{ID="5",Name="2008"},
                                          new{ID="6",Name="2007"},
                                            new{ID="7",Name="2006"},
                                              new{ID="8",Name="2005"},
                                                new{ID="9",Name="2004"},
                                                  new{ID="3",Name="2003"},
                                                   new{ID="3",Name="2002"},
                                                    new{ID="3",Name="2001"},
                                                    new{ID="3",Name="2000"},

                                      },
                              "ID", "Name", 1);
        ViewData["list"] = listYear;

The code below is used in the view

@using (Html.BeginForm()){   
     <p>
  Title: @Html.TextBox("SearchString")  
        @Html.DropDownList("list",ViewData["list"] as SelectList) 
     Genre: @Html.DropDownList("Towns", "All")  
     <input type="submit" value="Filter" /></p>
    }

Below is the code which was used for the textbox

  if (!String.IsNullOrEmpty(year))
        {
            car = Cars.Where(s => s.Year.Contains(year));
        }
  • 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-31T20:47:00+00:00Added an answer on May 31, 2026 at 8:47 pm

    Looks like you are trying to select a value from a selectlist and send that value to the controller. First off, I’d suggest you use a ViewModel instead of magic strings. You should modify your View to accept the new ViewModel and then post the model to your action. It’s easy, cleaner and more maintainable.

    Here is what your model would look like

    public class VehicleYearsViewModel {
    
        public SelectList VehicleYears { get; set; }
        public int SelectedYear { get; set; }
    
        public VehicleYearsViewModel() {
            VehicleYears = new SelectList(new[]
                                      {
                                          new{ID="1",Name="2012"},
                                          new{ID="2",Name="2011"},
                                          new{ID="3",Name="2010"},
                                          new{ID="4",Name="2009"},
                                          new{ID="5",Name="2008"},
                                          new{ID="6",Name="2007"},
                                          new{ID="7",Name="2006"},
                                          new{ID="8",Name="2005"},
                                          new{ID="9",Name="2004"},
                                          new{ID="3",Name="2003"},
                                          new{ID="3",Name="2002"},
                                          new{ID="3",Name="2001"},
                                          new{ID="3",Name="2000"}
                                      }
        }
    }
    

    Your View then would look like so:

    @YourAppName.Models.VehicleYearsViewModel
    
    @using (Html.BeginForm()){
        @Html.ValidationSummary(true)
        @Html.DropDownListFor(model => model.SelectedYear, Model.VehicleYears, "ID", "Name", 1))
        <input type="submit" value="OK" />
    }
    

    Your controller action would accept the model and can make use of the selected value as an int datatype.
    I’m just guessing since your controller action isn’t posted but this is pretty much what it would look like:

    public class HomeController : Controller {
    
        public ActionResult Index() {
            var model = new VehicleYearsViewModel();
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(VehicleYearsViewModel model) {
            if(ModelState.IsValid) {
                // you can get selected year like so
                int selectedYear = model.SelectedYear;
                // ... your code here to do whatever with selectedYear
            }
            return View(model);
         }   
    }
    

    Hope this helps

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

Sidebar

Related Questions

Our team is migrating from VSS 6.0 to TFS 2008 to be used for
Just migrating from PHP 5.2 to 5.3, lot of hard work! Is the following
I am migrating from ASP.NET 1.1 and I used to subscribe to page events
We're currently migrating from NMock2 to RhinoMocks and we're having trouble replicating this code:
I am getting error below on every action after migrating from JSF 1.2 and
I am having troubles migrating from OC4J 10.1.2.3 to 10.1.3.1.4. The problem is for
We are migrating from asp classic to .net. Unfortunately they named all of the
I started migrating from cucumber + webrat to cucumber + capybara. Now the behavior
Are there tools for migrating from one virtual machine type to another? E.g let's
I am migrating from Ubuntu 10.04 to Ubuntu 11.10 (oneiric), but I have problems

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.