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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:41:34+00:00 2026-05-31T01:41:34+00:00

I’m creating a search application in mvc3 where i have 2 tables : 1.State

  • 0

I’m creating a search application in mvc3 where i have 2 tables :
1.State :Id(pk) and state_name
2.District:Id(pk),s_id(f.k.), District_name

I am using code first and EF and have database created for it called Search

I want my index to show all states in drop down list

following is my State.cs code

public partial class State
{
    public State()
    {
        this.Districts = new HashSet<District>();
        this.Search_master = new HashSet<Search_master>();
    }

    public int Id { get; set; }
    public string State_name { get; set; }

    public virtual ICollection<District> Districts { get; set; }}

this is my District class:

 public partial class District
{
    public District()
    {
        this.Search_master = new HashSet<Search_master>();
    }

    public int Id { get; set; }
    public string District_name { get; set; }
    public int StateId { get; set; }

    public virtual State State { get; set; } }

I also created one viewmodel for state and district….

  public class State_district
{
    public string selectedstate { get; set; }
    public IEnumerable<State> states { get; set; }
    public string selecteddistrict { get; set; }
    public IEnumerable<District> districts { get; set; }
}

inside controller i have written:

 public ActionResult Index()
    {
        var model = new State_district { states = db.States, districts = db.Districts };
        return View(model);}

in the view:

 <div class="editor-field"  id="districtID">*Select State:-
         @Html.DropDownListFor(x => x.states, new SelectList(Model.states, "Id", "State_Name"))
        </div>

With this i am able to see my 1st ddl but how can i bind it with my second list…..

I need code that help me to show me district from selected state only…..can anybody help me with the jQuery code…

thank you in advance!!

  • 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-31T01:41:35+00:00Added an answer on May 31, 2026 at 1:41 am

    You are trying to implement a dynamic/cascading dropdown and there are a lot of resources on the internet which can help you with this:

    • Such as this
    • Or this
    • And much more on google

    A simple non-AJAX implementation would work like this:

    You have your first dropdown, created normally:

    @Html.DropDownListFor(x => x.states, new SelectList(Model.states, "Id", "State_Name"), new {id = "states"})
    

    Your second dropdown, or dependent dropdown, will need to store some information about which parent item each item is associated with. e.g.:

    <select name="selecteddistrict" id="districts">
    @foreach (var district in Model.districts)
    {
        <option value="@district.Id" data-state-id="@district.StateId">@district.District_name</option>
    }
    </select>
    

    Notice how each entry in the dropdown has an additional attribute data-state-id. We need this information in order to know which values to show given the current active State.

    And then you would add an event handler which handles the change event on your primary dropdown, and update the dependent dropdown accordingly:

    <script type="text/javascript">
    
    $(function () {
        $("#states").change(onStateChange).change(); // fire for initial setup
    });
    
    function onStateChange() {
        var districtDropdown = $('#districts'); 
        districtDropdown.find('option').hide();
        var districtsForState = $(districtDropdown.find('option[data-state-id=' + $(this).val() + ']'));
        districtsForState.show();
        districtDropdown.val(districtsForState.val());
    }
    
    </script>
    

    Please note the code provided are to demonstrate the concept only.

    • 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 this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.