Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 23rd, 2010, 05:12 AM
Registered User
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Apply change function on one dropdown to fill another dropdown box

I am coding a small project using MVC 2 after read chapter 1 of "Professional ASP.NET MVC 2". On one of my views, there are two Select boxes, box1 is for selecting Fields, box2 for values. When the selected item of box1 changes, the values set in box2 will be changed accordingly.

Have some basic understanding from the book, I read "http://stackoverflow.com/questions/2500949/is-it-possible-to-bind-data-asynchronously-between-two-dropdownlists-in-a-view", then create the funciton as below:

In the controller, the method is:
 public ActionResult getValByFld(string thefld)
            List<string> thevals = new List<string>();
            string sql = "  ";
            if (thefld == "prio")
                sql = "select acode from pas. patcodes where acode <> ' ' and tcode ='TP' and ptcoactv ='A' order by acode";
            else if (thefld == "purch")
                sql = "select acode from pas. patcodes where acode <> ' ' and tcode ='HP' and ptcoactv ='A' order by acode";
            else if (thefld == "serv")
                sql = "select distinct wprhat from pas.watproaf where wprhat <> ' ' order by wprhat ";

            database = DatabaseFactory.CreateDatabase("HEALTH-SOURCE");
            reader = database.ExecuteReader(CommandType.Text, sql);
            while (reader.Read())
            return Json(thevals);
the view template is:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ESPItry.Models.ESPIAddLev1ViewModel>" %>

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

    <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="/Scripts/jQuery-1.4.1.js" type="text/javascript"></script>

    <script type="text/javascript">
    $(function() { 
    $('select#fldExclu').change(function() { 
        var fldId = $(this).val();
        $.post('/ESPITrys/getValByFld', { thefld: fldId }, function(thevals) { 
            var valSelect = $('select#valExclu');
            // loop through thevals and fill the dropdown 
            $(thevals).each(function(item) { 
                    '<option value="' + item + '">'
                    +   '</option>'); 


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

    <h2>Add New Level 1 Exclusion</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.objLev1Exclu.fldExclu)%>
            <div class="editor-field">
                 <%: Html.DropDownListFor(m => m.objLev1Exclu.fldExclu, Model.flds) %>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.objLev1Exclu.valExclu)%>
             <div class="editor-field">
                 <%: Html.DropDownListFor(m => m.objLev1Exclu.valExclu, Model.vals) %>
                <input type="submit" value="Create" />

    <% } %>

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

When run, the view can be opened with box2 being empty. But when change box1's value, box2 can't be filled and no error is showed. Please let me know what goes wrong in my code.

Thank you very much

Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDown Dynamic Value Change Master/Detail moizshabbir PHP How-To 1 May 9th, 2008 06:24 AM
How to integrate search function n dropdown box jun9nah ASP.NET 1.0 and 1.1 Basics 1 July 25th, 2006 06:51 AM
How to integrate search function n a dropdown box jun9nah VB.NET 0 July 24th, 2006 10:12 PM
Fill Dropdown using AJAX sethumail2000 BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 March 6th, 2006 01:19 PM
Fill Dropdown using AJAX sethumail2000 ASP.NET 2.0 Professional 0 March 6th, 2006 09:00 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.