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

  • Home
  • SEARCH
  • 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 8629643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:51:28+00:00 2026-06-12T08:51:28+00:00

I’m new to stackoverflow as well as to jquery/javascript. I’ve been searching all day

  • 0

I’m new to stackoverflow as well as to jquery/javascript. I’ve been searching all day for different ways to add cascading drop down lists to my current project and have yet to find a way that has worked for me. Most of my finding have been from out of date and based upon MVC 2 to webforms to older technologies. I did find a few tutorials and posts based upon MVC 3/4 that have helped but I’m still about to chunk my mouse at my computer screen.

Some links that I’ve looked at for help are:
Radu Enuca’s Tutorial on cascading dropdown lists
and
Rick_Anderson’s Tutorial

Some background on the project:

I’m creating a work ticket system for employees to submit their daily time to the office. I have a controller, view, and jquery script listed below.

Controller

public class WorkTicketController : Controller
{
    private Context db = new Context();

    public ActionResult GetClientReps(int id)
    {
        var Reps = from c in db.ClientReps
                   where c.ClientID == id
                   select c;

        List<SelectListItem> clientReps = new List<SelectListItem>();

        foreach (var item in Reps)
        {
            string clientRepId = item.ClientRepID.ToString();

            string clientRepName = item.FirstName + " " + item.LastName;

            clientReps.Add(new SelectListItem(){ Value = clientRepId, Text = clientRepName });
        }

        var List = new SelectList(clientReps, "Value", "Text");

        return Json(List, JsonRequestBehavior.AllowGet);
    }

    public ActionResult Create()
    {
        ViewBag.Clients = GetGlobalItems.ClientList();
        ViewBag.Supervisors = GetGlobalItems.UserListByRole("Supervisor");

        return View();
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}
}

View

@model NewbyPortal.Models.WorkTicket

@{
ViewBag.Title = "Create";
}
<article>
<div class="linearBg1">Create Daily Work Ticket</div>
<br />

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <div class="linearBg1">
        General Information
    </div>
    <div class="section-span-body">
        <table>
            <tr class="empTableRowBody2">
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.DateWorked) *
                </th>
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.PayKey)
                </th>
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.PONumber)
                </th>
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.WONumber) *
                </th>
            </tr>
            <tr>
                <td>
                    @Html.EditorFor(Model => Model.DateWorked)
                </td>
                <td>
                    @Html.EditorFor(Model => Model.PayKey)
                </td>
                <td>
                    @Html.EditorFor(Model => Model.PONumber)
                </td>
                <td>
                    @Html.EditorFor(Model => Model.WONumber)
                </td>
            </tr>
            <tr class="empTableRowBody2">
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.ProjectId)
                </th>
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.ClientID)
                </th>
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.ClientRepID)
                </th>
                <th class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.SupervisorID) *
                </th>
            </tr>
            <tr>
                <td>
                    @Html.TextBoxFor(Model => Model.ProjectId)
                </td>
                <td>
                    @Html.DropDownList("ClientID", ViewBag.ClientID as SelectList, "Select a Client", new { id = "drop1" })
                </td>
                <td>
                    <select id="drop2"></select>
                </td>
                <td>
                    @Html.DropDownList("SupervisorID")
                </td>
            </tr>
            <tr class="empTableRowBody2">
                <th colspan="2" class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.JobLocation) *
                </th>
                <th colspan="2" class="empTableRowBody2">
                    @Html.LabelFor(Model => Model.JobDescription) *
                </th>
            </tr>
            <tr>
                <td colspan="2">
                    @Html.TextBoxFor(Model => Model.JobLocation, new{@class="textboxlengthlong"})
                </td>
                <td colspan="2">
                    @Html.TextBoxFor(Model => Model.JobDescription, new{@class="textboxlengthlong"})
                </td>
            </tr>
        </table>
    </div>
    <div class="section-span-footer"></div>
    <p>
        <input type="submit" value="Next" />
    </p>
}

Script

<script type="text/javascript">
$(document).ready(function () {
    $("#drop1").change(function () {
        var id = $(this).val();
        $.getJSON("/WorkTicket/GetClientReps/", { id: id },
            function (data) {
            var select = $("#drop2");
            select.empty();
            select.append($('<option/>', {
                value: 0,
                text: "Select a Client Rep"
            }));
            $.each(data, function (index, data) {

                select.append($('<option/>', {
                    value: data.Value,
                    text: data.Text
                }));
            });
        });
    });
});    
</script>

When I choose a Client from the Client drop down list, nothing happens for the Client Rep drop down list. I know it must be something obvious that I’m missing but at this point I don’t mind looking like an idiot to solve this frustrating problem.

I have verified that that other jquery works in my project so I don’t have anything disabled as far as I can tell.

Thank you in advance for the help!

T

Update

So I have made one step forward it appears. I started moving my script placement around on the view. I tried the top and the bottom of the page. I also reviewed the layout page to make sure I was link to the right jquery libraries. That all checked out but it got me thinking so I moved my script into it’s own custom.js file and linked to it on the layout page and the cascading drop down lists started working.

My next question is why? and should I leave it the way it is now? Thanks!

T

  • 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-12T08:51:29+00:00Added an answer on June 12, 2026 at 8:51 am

    Ok -> The Viewbag should be Viewbag.Clients, not Viewbag.ClientID

    @Html.DropDownList("ClientID", ViewBag.ClientID as SelectList, "Select a Client", new { id = "drop1" })
    
    <select id="drop2"></select>
    

    And the same for this…

    @Html.DropDownList("SupervisorID")
    

    jQuery code is Ok.

    Try this, and then fill with your code:

    Create.cshtml

    @{
        ViewBag.Title = "Create";
    }
    
    <script type="text/javascript">
        $(document).ready(function () {
            $("#drop1").change(function () {
                var id = $(this).val();
                $.getJSON("/WorkTicket/GetClientReps/", { id: id },
                function (data) {
                    var select = $("#drop2");
                    select.empty();
                    select.append($('<option/>', {
                        value: 0,
                        text: "Select a Client Rep"
                    }));
                    $.each(data, function (index, data) {
    
                        select.append($('<option/>', {
                            value: data.Value,
                            text: data.Text
                        }));
                    });
                });
            });
        });    
    </script>
    
    
    
    
    <div class="linearBg1">Create Daily Work Ticket</div>
    <br />
    
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
    
        @Html.DropDownList("ClientID", ViewBag.Clients as SelectList, "Select a Client", new { id = "drop1" })
        <select id="drop2"></select>
    
    
    
        <div class="section-span-footer"></div>
        <p>
            <input type="submit" value="Next" />
        </p>
    }
    

    WorkTicketController

    public class WorkTicketController : Controller
    {
        //
        // GET: /WorkTicket/
    
        public ActionResult Index()
        {
            return View();
        }
    
        private Context db = new Context();
    
        public ActionResult GetClientReps(int id)
        {
            /*var Reps = from c in db.ClientReps
                       where c.ClientID == id
                       select c;
            */
            List<SelectListItem> clientReps = new List<SelectListItem>();
    
            /*foreach (var item in Reps)
            {
                string clientRepId = item.ClientRepID.ToString();
    
                string clientRepName = item.FirstName + " " + item.LastName;
    
                clientReps.Add(new SelectListItem() { Value = clientRepId, Text = clientRepName });
            }*/
    
            clientReps.Add(new SelectListItem() { Value = "10", Text = "name" });
    
            var List = new SelectList(clientReps, "Value", "Text");
    
            return Json(List, JsonRequestBehavior.AllowGet);
        }
    
        public ActionResult Create()
        {
            List<SelectListItem> clientReps = new List<SelectListItem>();
            clientReps.Add(new SelectListItem() { Value = "1", Text = "client 1" });
            clientReps.Add(new SelectListItem() { Value = "2", Text = "client 2" });
            ViewBag.Clients = new SelectList(clientReps, "Value", "Text");
            //ViewBag.Supervisors = GetGlobalItems.UserListByRole("Supervisor");
    
            return View();
        }
    
        protected override void Dispose(bool disposing)
        {
            //db.Dispose();
            base.Dispose(disposing);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I've tracked down a weird MySQL problem to the two different ways I was
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.