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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:37:15+00:00 2026-05-11T13:37:15+00:00

I want extension of the following code for selected value in edit view. take

  • 0

I want extension of the following code for selected value in edit view. take a case of country -> state -> city.

i have script CascadingDropDownList.js:

function bindDropDownList(e, targetDropDownList)  {     var key = this.value;     var allOptions = targetDropDownList.allOptions;     var option;     var newOption;     targetDropDownList.options.length = 0;      for (var i=0; i < allOptions.length; i++)      {         option = allOptions[i];         if (option.key == key)          {             newOption = new Option(option.text, option.value);             targetDropDownList.options.add(newOption);         }     } } 

and I have a helper class:

public static class JavaScriptExtensions {     public static string CascadingDropDownList(this HtmlHelper helper, string name, string associatedDropDownList)     {         var sb = new StringBuilder();          // render select tag         sb.AppendFormat('<select name='{0}' id='{0}'></select>', name);         sb.AppendLine();          // render data array         sb.AppendLine('<script type='text/javascript'>');         var data = (CascadingSelectList)helper.ViewDataContainer.ViewData[name];         var listItems = data.GetListItems();         var colArray = new List<string>();         foreach (var item in listItems)             colArray.Add(String.Format('{{key:'{0}',value:'{1}',text:'{2}'}}', item.Key, item.Value, item.Text));         var jsArray = String.Join(',', colArray.ToArray());         sb.AppendFormat('$get('{0}').allOptions=[{1}];', name, jsArray);         sb.AppendLine();         sb.AppendFormat('$addHandler($get('{0}'), 'change', Function.createCallback(bindDropDownList, $get('{1}')));', associatedDropDownList, name);         sb.AppendLine();         sb.AppendLine('</script>');          return sb.ToString();      } }  public class CascadingSelectList {     private IEnumerable _items;     private string _dataKeyField;     private string _dataValueField;     private string _dataTextField;      public CascadingSelectList(IEnumerable items, string dataKeyField, string dataValueField, string dataTextField)     {         _items = items;         _dataKeyField = dataKeyField;         _dataValueField = dataValueField;         _dataTextField = dataTextField;     }      public List<CascadingListItem> GetListItems()     {         var listItems = new List<CascadingListItem>();         foreach (var item in _items)         {             var key = DataBinder.GetPropertyValue(item, _dataKeyField).ToString();             var value = DataBinder.GetPropertyValue(item, _dataValueField).ToString();             var text = DataBinder.GetPropertyValue(item, _dataTextField).ToString();             listItems.Add(new CascadingListItem(key, value, text));         }         return listItems;     } } public class CascadingListItem {     public CascadingListItem(string key, string value, string text)     {         this.Key = key;         this.Value = value;         this.Text = text;     }      public string Key { get; set; }     public string Value { get; set; }     public string Text { get; set; } } 
  • 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. 2026-05-11T13:37:16+00:00Added an answer on May 11, 2026 at 1:37 pm

    I have achieved it. Where selected value is in Hidden Filed.

    And make sure that your controller method returns json object with two fields.

    <script type='text/javascript'>  var ddlCountry; var ddlStateID;  function pageLoad() {     ddlStateID = $get('StateID');     ddlCountry = $get('CountryID');     $addHandler(ddlCountry, 'change', bindOptions);     bindOptions(); } function bindOptions() {     ddlStateID.options.length = 0;     var CountryID = ddlCountry.value;     var stateSelected = document.getElementById('StateSelected').value;     if (CountryID) {         var url = '/Student/States/' + CountryID;         $.getJSON(url, null, function(data) {             $('#StateID').empty();             $.each(data, function(index, optionData) {                 if (stateSelected == optionData.ID) {                     $('#StateID').append('<option value=''                     + optionData.ID                     + '' selected>' + optionData.Name                     + '</option>');                 }                 else {                     $('#StateID').append('<option value=''                     + optionData.ID                     + ''>' + optionData.Name                     + '</option>');                 };             });         });     }; } 

    ADDED controller (My url is student/state i.e. in student controller I put following code)

    public ActionResult States(int id)     {         var states = db.states.Select(s => new { ID = s.ID, Name = s.Name }).OrderBy(s=>s.Name).ToList();         return Json(states);     } 

    You can return any number of fields but if any of them will have a null value then it will not converted in json object. I am not sure about this but it wasn’t working when I return whole state object. At that time states table was having field called deletedby and it was containing null values!. But when I return only two ‘needed’ fields then it is working properly!

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

Sidebar

Related Questions

What I want to do is trigger a function from an extension/GM Script once
I have an Xelement containing a number of elements. I have the following code
I have a simple script to call select-string for a specific file extension. I
I want to add a file type extension to the Open Dialog of Excel
I want to write a Songbird extension binds the multimedia keys available on all
i want to find the mime-type for a given file extension on an IIS
I want to test ASP.NET applications to get the feel for the MVC extension
If you want to open an msg Email file with the extension .msg, Outlook
Title says it mostly. I want to add a simple extension method to the
For statistical reasons, I want an extensive analysis from a dataset. I already have

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.