2

I have two select in view filling through ViewData that are,

In view,

@{

    IEnumerable<Region> region = ViewData["regions"] as IEnumerable<Region>;
    IEnumerable<City> city = ViewData["cities"] as IEnumerable<City>;
}

  <tfoot>
            <tr>
                <td><input type="text" id="txtTribeName" /></td>
                <td>
                    <select id="ddlregion" name="RegionId">
                        <option value="0">Select Region</option>
                        @foreach (Region re in region)
                        {
                            <option value="@re.RegionID">@re.RegionName</option>
                        }
                    </select>
                </td>
                <td>
                    <select id="ddlcity" name="CityId">
                        <option value="0">Select City</option>
                        @foreach (City re in city)
                        {
                            <option value="@re.CityID">@re.CityName</option>
                        }
                    </select>
                </td>
                @*<td>@Html.dropDropDownListFor(ViewData["regions"] as IEnumerable<SelectListItem>)</td>*@
                <td><input type="button" id="btnAdd" value="Add" /></td>
            </tr>
        </tfoot>

my script is like,

$("body").on("change", "#ddlregion", function () {


        });

In script using IEnumerable<City> city can i sort it according to region

Hopes for suggestions.

Edit:

my controller passing region and city from Ef like,

 TribeEntities Db = new TribeEntities();
    ViewData["regions"] = Db.Regions;
    ViewData["cities"] = Db.Cities;
5
  • to clarify, you want to filter City, depending on the Region selected? By filter, you only want to sort? Or do you want to hide and show options? Commented Mar 2, 2020 at 6:54
  • i want to filter city based on region. when i select particular region city drop down only shows city that comes under that region Commented Mar 2, 2020 at 7:01
  • okay, got it. Will you include the City Class (code) in your question. Thanks. Commented Mar 2, 2020 at 7:03
  • City class is actually Entity framework city table that i am using directly. check my edited question. Commented Mar 2, 2020 at 7:08
  • 1
    City table has city id , cityname and regionid. Where as region table has only regionid and regionname Commented Mar 2, 2020 at 7:10

1 Answer 1

2

In your City dropdown, we need to add a data-attribute. I used data-region. Use this as your City dropdown;

<select id="ddlcity" name="CityId">
   <option  value="0">Select City</option>
   @foreach (City re in city)
   {
      <option data-region="@re.RegionID" value="@re.CityID">@re.CityName</option>
   }
</select>

Then, we need to loop through the options of City dropdown and show/hide them accordingly. Your script should be like;

$(document).on("change", "#ddlregion", function() {
   var selectedRegion = $(this).val();

   $("#ddlcity").find("option").each(function() {
      if ($(this).data("region") == selectedRegion) {
         $(this).show();
      } else {
         $(this).hide();
      }
   });
});

See static demo below;

$(document).on("change", "#ddlregion", function() {
  var selectedRegion = $(this).val();

  $("#ddlcity").find("option").each(function() {
    if ($(this).data("region") == selectedRegion) {
      $(this).show();
    } else {
      $(this).hide();
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="ddlregion" name="RegionId">
  <option value="0">Select Region</option>
  <option value="1">Region 1</option>
  <option value="2">Region 2</option>
  <option value="3">Region 3</option>
</select>

<select id="ddlcity" name="CityId">
  <option data-region="0">Select City</option>
  <option data-region="1">City 1 in Region 1</option>
  <option data-region="1">City 2 in Region 1</option>
  <option data-region="2">City 1 in Region 2</option>
  <option data-region="3">City 1 in Region 1</option>
  <option data-region="3">City 2 in Region 2</option>
  <option data-region="3">City 3 in Region 3</option>
  <option data-region="3">City 4 in Region 4</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.