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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:21:08+00:00 2026-05-17T15:21:08+00:00

I have 2 dropdownlists, the second (department) cascades from the first (division). I use

  • 0

I have 2 dropdownlists, the second (department) cascades from the first (division).
I use a jquery script to update the second drop down list.
When the user clicks submit, the underlying model is updated, apart from the new value for department.
2 ideas I have for fixing this are;
a) Generate a postback. Prefer not to do this because of bad user experience.
b) With a JQuery change event, make a call to server side and set a ViewData variable that I can use to update the field in the model later.

However there must be a better way.
This is the view;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" 
Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.EmployeeViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Add
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">

    <% using (Html.BeginForm("Add", "Employee")) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Add Employee</legend>
            <table>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Employee.UserName)%>
                   </td>                    
                    <td>
                <%: Html.EditorFor(model => model.Employee.UserName)%>
                <%: Html.ValidationMessageFor(model => model.Employee.UserName)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Division.DivisionId) %>
                   </td>                    
                    <td>
                <%: Html.DropDownListFor(model => model.Division.DivisionId, Model.DivisionSelectList, "<--Select-->")%>
                <%: Html.ValidationMessageFor(model => model.Division.DivisionId)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Department.DepartmentId) %>
                   </td>                    
                    <td>
                <%: Html.DropDownListFor(model => model.Department.DepartmentId, Model.DepartmentSelectList, "<--Select-->")%>
                <%: Html.ValidationMessageFor(model => model.Department.DepartmentId) %>
                    </td>
                </tr>

                <tr>
                    <td align="center" colspan="2" style="padding-top:20px;">
                    <input type="submit" value="Save" /></td>
                </tr>
            </table>
            <% if (ViewData["LastPerson"].ToString().Length > 0)
               { %>
                <p>
                   At time <% Response.Write(DateTime.Now.ToString("T")); %> 
                   - You have just entered <%Response.Write(ViewData["LastPerson"].ToString()); %>.
                </p>
             <%} %>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

    <script language="javascript" type="text/javascript">

        //Hook onto the DivisionId list's onchange event
        $("#Division_DivisionId").change(function () {
            //build the request url
            var url = '<%: Url.Content("~/")%>' + "Employee/GetDepartments";
            //fire off the request, passing it the id which is the DivisionId's selected item value
            $.getJSON(url, { divisionId: $("#Division_DivisionId").val() }, function (data) {
                //Clear the Department list
                $("#Department_DepartmentId").empty();
                $("#Department_DepartmentId").append("<option><--Select--></option>");
                //Foreach Department in the list, add a department option from the data returned
                $.each(data, function (index, optionData) {
                    $("#Department_DepartmentId").append(
                    "<option value='" + optionData.DepartmentId + "'>" +
                        optionData.DepartmentName + "</option>");
                });
           });
        }).change();

    </script>
  • 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-05-17T15:21:08+00:00Added an answer on May 17, 2026 at 3:21 pm

    I’d make a partial view that generates the second drop down. That would look something like this:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.WebUI.Models.EmployeeViewModel>" %>
    <%: Html.DropDownListFor(model => model.Department.DepartmentId, Model.DepartmentSelectList, "<--Select-->") %>
    

    It’s the Inherits attribute that make it strongly typed, your page above is also strongly typed.
    While you are at it, change this code:

    <%: Html.ValidationMessageFor(model => model.Department.DepartmentId) %>
    

    into:

    <%: Html.Partial("<TheNameOfYourDropDownPartial>", Model) %>
    

    Next, to add a change event to the division drop down change this line of code:

    <%: Html.DropDownListFor(model => model.Division.DivisionId, Model.DivisionSelectList, "<--Select-->")%>
    

    into:

    <%: Html.DropDownListFor(model => model.Division.DivisionId, Model.DivisionSelectList, "<--Select-->", new { onchange = "divisionChanged()" })%>
    

    And now, let’s add the divisionChanged() javascript function. It would look something like this:

    <script type="text/javascript">
        function divisionChanged() {
            var divisionId = $("select[name=Division.DivisionId]").val();
            $.ajax({
                data: { DivisionId: divisionId },
                dataType: "html",
                success: function (data, textStatus, xmlRequest) {
                    $("select[name=Department.DepartmentID]").replaceWith(data);
                },
                type: "POST",
                url: "/Employee/GetDepartmenDropDown});
        }
    </script>
    

    That function makes an ajax post to your GetDepartmentDropDown action and takes the returned html and replaces the department drop down with it.

    Your GetDepartmentDropDown will have to take whatever data you need to recreate the EmloyeeViewModel you passed in the view in the first place, (this data should be included in the data property in the ajax call in the javascript function) and then return something like this:

    var model = new EmployeeViewModel(DivisionId);
    return PartialView("<TheNameOfYourDropDownPartial>", model);
    

    You shouldn’t initialize the model in the return statement of course, it’s just there to give an idea of what needs to be done.

    That should do it I think. Then when you post the form the receiving action should be able to pick up the value.

    Hope it helps.

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

Sidebar

Related Questions

I have two dropdownlists ,selecting the first dropdown causes postback and second dropdown gets
I have two dropdownlists, the first is State & the second is City. I
I have a gridview 'GridViewMcm with dropdownlists while editing in first and second columns
I have a drop down list on my page & want the list items
I have 2 dropdownlists on my aspx page, second is filtered by the selection
I've got two DropDownLists. First is visible and second is not. I'd like to
I have a webcontrol with two dropdownlists on in. When you choose something from
I have a cascading dropdown list on my page. The first dropdown contains a
I have created 2 dropdownlists that are binded to a SQL DB and then
I have a site that uses a couple DropDownLists that are databound. I was

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.