473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Asp.net MVC2: filling cascading DDL using Ajax

I do not happen to know why this code does not work
the first list is filled with success but when I select an element nothing has changed

Expand|Select|Wrap|Line Numbers
  1. controller:
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using MvcGestionEcole.Models;
  8.  
  9. namespace MvcGestionEcole.Controllers
  10. {
  11.     public class DevoirController : Controller
  12.     {
  13.         //
  14.         // GET: /Devoir/
  15.  
  16.         public ActionResult Index()
  17.         {MvcGestionEcole.Models.NoteModel.Repository2 Repository = new MvcGestionEcole.Models.NoteModel.Repository2();
  18.             List<SelectListItem> items = new List<SelectListItem>();
  19.             items= Repository.GetNiveaux();
  20.             ViewData["Niveaux"] = items;
  21.  
  22.             return View();
  23.         }
  24.         public JsonResult  ListClasses(string id)
  25.         {
  26.             gestionecoleEntities db= new gestionecoleEntities();
  27.             List<SelectListItem> items = new List<SelectListItem>();
  28.             var v = (from p in db.classe
  29.                      where p.niveau_scolaire.Libelle_NS == id
  30.                      select new SelectListItem
  31.                      {
  32.                          Text = p.Libelle_C,
  33.  
  34.                          Value = p.Libelle_C
  35.                      });
  36.             //items.Add(v.ToList());
  37.             return this.Json(v.ToList(), JsonRequestBehavior.AllowGet);
  38.         }
  39.  
  40.     }
  41. }
  42. /////////
  43. View:
  44. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
  45.  
  46. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  47.  ListeNS_DEV
  48. </asp:Content>
  49.  
  50. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  51. <%--<script type="text/javascript" src="/Scripts/fillSelect.js"></script>--%>
  52.  
  53.  
  54.     <h2>ListeDesNiveauScolaire</h2>
  55.     <h2><%= Html.Encode(ViewData["Message"]) %></h2>
  56.      <% using (Html.BeginForm()) {%>
  57.         <%: Html.ValidationSummary(true) %>
  58.  
  59.         <fieldset>
  60.             <legend>Fields</legend>
  61.              <p>
  62.         <%: Html.ActionLink("Create New", "Create") %>
  63.     </p>
  64.                 <%: Html.DropDownList("Niveaux")%>
  65.                 <br/>
  66.               <select id="Classes" name="Classes" ></select>
  67.  
  68.         </fieldset>
  69. <% } %>
  70.  
  71.  <script type="text/javascript">
  72.      $(function () {
  73.          $('#Niveaux').change(function () {
  74.              $.getJSON("http://localhost:1903/Devoir/ListClasses/" + $(this).val(),
  75.               function (data) { $("#Classes").fillSelect(data); }
  76.               );
  77.          })
  78.      });
  79.  
  80.     </script>
  81.  
  82.  
  83.  
  84. </asp:Content>
  85.  
  86.  
  87. <asp:Content ID="Content3" ContentPlaceHolderID="head" runat="server">
  88. </asp:Content>
  89.  
  90. <asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  91. </asp:Content>
  92. /////////////////////
  93. fillSelect.js
  94. $.fn.clearSelect = function () {
  95.     return this.each(function () {
  96.         if (this.tagName == 'SELECT')
  97.             this.options.length = 0;
  98.     });
  99. }
  100.  
  101. $.fn.fillSelect = function (data) {
  102.     return this.clearSelect().each(function () {
  103.         if (this.tagName == 'SELECT') {
  104.             var dropdownList = this;
  105.             $.each(data, function (index, optionData) {
  106.                 var option = new Option(optionData.Text, optionData.Value);
  107.  
  108.                 if ($.browser.msie) {
  109.                     dropdownList.add(option);
  110.                 }
  111.                 else {
  112.                     dropdownList.add(option, null);
  113.                 }
  114.             });
  115.         }
  116.     });
  117. }
Apr 4 '13 #1
1 1458
r035198x
13,262 8TB
1.) Use code tags when posting code.
2.) Do some basic debugging on your own first. Put alerts in your javascript and see if the Javascript is being called at all. Also put some WriteLine statements in the server code to see if it's being called at all and print out the values returned by the server code. That's how you narrow down the source of the problem.
Apr 4 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Senthil | last post by:
Hi all I'm new in ajax. How to upload a image using ajax..
5
by: Martin | last post by:
Hello NG, I've been doing some AJAX for a few weeks now. The basics worked fine so far, but now I've got the following problem which I can't solve: With AJAX you typically update/replace only...
3
by: noballack | last post by:
I've got a problem, I'm working with Ajax in a web with a form with a list of checkbox added to the form via an Ajax.Updater method. These added checkboxs are not been sended by the form if I use...
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
3
by: bss | last post by:
Dear All, I have developing a French website using PHP & Ajax. In that I tried to display some French texts from mysql database using Ajax. Form local I got the text from db with Correct...
13
by: Marvin Zhang | last post by:
Hi, I'm not familiar with web programming, but I have a problem here. I have a page. When a user click one button on it, I will use AJAX to request a PHP script which will do a bunch of tasks,...
8
by: Bhuwan Bhaskar | last post by:
Hi, Can we access database (SQL) using AJAX ? Thanks, Bhuwan
9
by: Tim Johnson | last post by:
I'm an experienced web/CGI programmer, but new to ajax. I'd like to review some examples of how to post data from a form to server using AJAX. I have already created some examples successfully...
1
by: visweswaran2830 | last post by:
Hi, I tried cascading dropdown in asp.net using ajax. I have tried with 3 dropdown control. The scenario is First dropdown is loaded when page gets loaded and the second dropdown is loaded based...
1
by: visweswaran2830 | last post by:
Hi, I have four cascading dropdown control and now I want to load a gird data based on the value selected on that dropdown. This should be done by using ajax concepts..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.