I am a novice developer, but I came across a case that I cannot solve. I am using ASP.NET MVC with C # plus some javascript (jQuery, JSON ...).
I need to populate a dropdown based on the value selected in another. It seems simple (and I know it), but this particular case eludes me. I have lost many hours and I am still banging my head. If anyone can help, I will be very grateful.
My code is as follows:
I have an object of the class "Task", which has both phase and order. Orders available to the user in the second drop-down list will depend on the phase in which this task was set in the drop-down list. The order is just Int32 (the order from the order is in the schedule, and not in the business order)
The first DropDown uses this IF to check if the session is null, then it processes the default message or brings the selected default value.
<% if (TempData["TaskObject"] != null) { var Phases = new SelectList(ViewData["phaseList"] as List<Phase>, "Phase_ID", "Phase_Name", ((Task)TempData["TaskObject"]).Phase_ID.ToString()); %> <%=Html.DropDownList("Phase_ID", Phases, new { @class = "textbox"})%> <%}%> <%else {%> <%=Html.DropDownList("Phase_ID", (new SelectList((List<Phase>)ViewData["phaseList"], "Phase_ID", "Phase_Name")), "Please Choose...", new { @class = "textbox"})%> <%} %>
The second DropDown is in many ways similar to the first, but its SelectList is a list of lists. Each list contains the options for each choice in the first DropDown. There will not be many, so do not worry about it.
<%if (TempData["TaskObject"] != null) { var orderList = (ViewData["orderList"] as List<List<int>>)[0]; var orders = new SelectList(orderList, ((Task)TempData["TaskObject"]).Phase_ID.ToString()); %> <%=Html.DropDownList("Order_ID", orders, new { @class = "textbox"})%> <%}%> <%else {%> <%=Html.DropDownList("Order_ID", (new SelectList((List<int>)ViewData["phaseList"], "Phase_ID", "Phase_Name")), "Please Choose...", new { @class = "textbox"})%> <%} %>
Now the question arises: how to get the second DropDown to change its values based on the selected value in the first DropDown? I am very poorly versed in JavaScript (started to learn, though). We use jQuery, JSON in the project.
Thank you in advance for your attention.