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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:46:13+00:00 2026-06-09T12:46:13+00:00

I am trying to convert a poorly developed C# web app to MVC2, and

  • 0

I am trying to convert a poorly developed C# web app to MVC2, and I am having difficulties trying to build a cascading dropdown. This is the code:

public IEnumerable<SelectListItem> SchoolList
    {
        get
        {
            DataTable dt = ClassModels.GetSchools();
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new SelectListItem
                {
                    Text = Convert.ToString(row["School"]),
                    Value = Convert.ToString(row["SID"]),
                });
            }
            return list;
        }
    }

    public IEnumerable<SelectListItem> DepartmentList
    {
        get
        {
            DataTable dt = DomData.GetDepartments(this is where the selected SID goes)
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new SelectListItem
                {
                    Text = Convert.ToString(row["Department"]),
                    Value = Convert.ToString(row["DID"]),
                });
            }
            return list;
        }
    }

This is the model for the Departments

public static DataTable GetDepartments(int id)
    {
        string sql = string.Format(@"SELECT d.department, d.did
            FROM dept d
            WHERE d.did IN (SELECT DISTINCT(DID) FROM CDS WHERE SID = '{0}')
            ORDER BY department", id);

        DataTable db_table = new DataTable("departments");
        SqlDataAdapter db_adapter = new SqlDataAdapter(sql, iau_conxn_string);

        db_adapter.Fill(db_table);
        //ddl_dep.DataSource = db_table;
        //ddl_dep.DataValueField = "did";
        //ddl_dep.DataTextField = "department";
        //ddl_dep.DataBind();
        return db_table;
    }

Sadly, I have to do this without AJAX or jQuery.

  • 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-06-09T12:46:15+00:00Added an answer on June 9, 2026 at 12:46 pm

    Since you cannot use AJAX but only pure javascript you could subscribe to the onchange event of the first dropdown and then submit the form in order to populate the second dropdown.

    But before putting this into action let’s start by defining a view model. In my example I will obviously remove all the noise that was present in your question (things like DataTables and data access specific code and simply hardcode the values so that the answer be more general, so simply replace those hardcoded values with a call to your data access method):

    public class MyViewModel
    {
        public MyViewModel()
        {
            Departments = Enumerable.Empty<SelectListItem>();
        }
    
        public int? SchoolId { get; set; }
        public IEnumerable<SelectListItem> SchoolList
        {
            get
            {
                // TODO: fetch the schools from a DB or something
                return new[]
                {
                    new SelectListItem { Value = "1", Text = "school 1" },
                    new SelectListItem { Value = "2", Text = "school 2" },
                };
            }
        }
    
        public int? DepartmentId { get; set; }
        public IEnumerable<SelectListItem> Departments { get; set; }
    }
    

    then a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Departments(MyViewModel model)
        {
            // TODO: use model.SchoolId to fetch the corresponding departments
            // from your database or something
            model.Departments = new[]
            {
                new SelectListItem { Value = "1", Text = "department 1 for school id " + model.SchoolId },
                new SelectListItem { Value = "2", Text = "department 2 for school id " + model.SchoolId },
                new SelectListItem { Value = "3", Text = "department 3 for school id " + model.SchoolId },
            };
            return View("Index", model);
        }
    }
    

    and finally a view:

    <%@ Page 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewPage<MyViewModel>" 
    %>
    <% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myform", data_departments_url = Url.Action("Departments") })) { %>
        <div>
            <%= Html.LabelFor(x => x.SchoolId) %>
            <%= Html.DropDownListFor(
                x => x.SchoolId, 
                Model.SchoolList, 
                "-- School --",
                new { id = "school" }
            ) %>
        </div>
    
        <div>
            <%= Html.LabelFor(x => x.DepartmentId) %>
            <%= Html.DropDownListFor(
                x => x.DepartmentId, 
                Model.Departments,
                "-- Department --"
            ) %>
        </div>
    <% } %>
    

    and the last bit is of course javascript to subscribe to the onchange event of the first dropdown and submit the form to the server in order to populate the second dropdown:

    window.onload = function () {
        document.getElementById('school').onchange = function () {
            if (this.value) {
                var form = document.getElementById('myform');
                form.action = form.getAttribute('data-departments-url');
                form.submit();
            }
        };
    };
    

    And for those that don’t have the same constraints as you and can use AJAX and jQuery take a look at the following answer: https://stackoverflow.com/a/4459084/29407

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

Sidebar

Related Questions

Trying to convert this c code into MIPS and run it in SPIM. int
I am trying to convert this piece of html code to javascript so that
I trying to convert this C# code VB.Net. its giving syntax error. C# @{
Trying to convert the following but am having difficulty. Can anyone see where I'm
im trying to convert a date format into a new format in this example
Im trying to convert some Delphi code to c# and I've come across a
Trying to convert this: HDC hdc = CreateCompatibleDC(NULL); HBITMAP cross = (HBITMAP)LoadImage(NULL, _(c:\\captureqwsx.bmp) ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
Trying to convert a string to NSURL and this is not happening. barcodeTextLabel.text =
I am trying convert this JPA QL to criteria builder. JBoss 6.0. SELECT ba
Trying to convert some VB to C#... (learning C#, too). I have some code

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.