title: How to bind JSON data to dropdownlist in Asp.net MVC using jQuery
body: <p>I am trying to bind JSON Data to dropdown list </p>
  
  <p>My Scenario is I want to get data and Bind to dynamic dropdown list,</p>
  
  <p>In Seperate Class, I have used linq to get data like</p>
  
  <pre><code>    public SelectList getProjects()
      {
          IEnumerable&lt;SelectListItem&gt; projectslist = (from proj in res.PROJECTs where proj.IS_DELETED == "N" select proj).AsEnumerable().Select(projt =&gt; new SelectListItem() { Text = projt.NAME, Value = projt.ID.ToString() });
          return new SelectList(projectslist, "Value", "Text", PROJ_ID);
      }
  </code></pre>
  
  <p>In Controller:</p>
  
  <pre><code>ViewBag.ProjectList=(from proj in res.PROJECTs where proj.IS_DELETED == "N" select proj).AsEnumerable().Select(projt =&gt; new SelectListItem() { Text = projt.NAME, Value = projt.ID.ToString() });
  </code></pre>
  
  <p>In View:</p>
  
  <pre><code>    @for (int i = 0; i &lt;2; i++)
      {                                    {
     @Html.DropDownListFor(m =&gt; m.GetTimeSheetDetails[i].PROJ_ID, (SelectList)ViewBag.ProjectList, "-- Choose a Project --", new { @class = "ddlProjectvalue" })
      }
  </code></pre>
  
  <p>Now, I am trying for like if  we have three dropdownlist, we select a list item in first dropdown list should not show in second dropdown list, and in third dropdown list should not show both previous selected list items for that i have writtern script like:</p>
  
  <pre><code>     &lt;script&gt;
      $(document).ready(function () {
          $('.ddlProjectvalue').change(function () {
              debugger;
              var selectedValue = $(this).val();
              if (selectedValue != null &amp;&amp; selectedValue != '') {
                  debugger;
                  $.ajax({
  
                      type: "GET",
                      contentType: "application/json; charset=utf-8",
                      url: "/Employer/GetDDLData?selectedValue="+selectedValue,
                      data: "{}",
                      dataType: "Json",
                      success: function (data) {
  
                          // first remove the current options if any
                          $('.ddlProjectvalue').find('option').remove();
  
                          // next iterate thru your object adding each option to the drop down\    
                          $(data).each(function (index, item) { // GETTING ERROR HERE
                              debugger;
  
                              $('.ddlProjectvalue').append($('&lt;option&gt;&lt;/option&gt;').val(item.Value).html(item.Text));
                          });
                      },
                      error: function ajaxError(response) {
                      alert(response.status + ' ' + response.statusText);
                  }
              });
          }
        });
      });
     &lt;/script&gt;
  </code></pre>
  
  <p>and I am returning JSON Data from Controller:</p>
  
  <pre><code>    public ActionResult GetDDLData(string selectedValue)
      {
          int projectid = Convert.ToInt32(selectedValue);
  
          IEnumerable&lt;SelectListItem&gt; projectslist = (from proj in db.PROJECTs where proj.IS_DELETED == "N" &amp;&amp; proj.ID != projectid select proj).AsEnumerable().Select(projt =&gt; new SelectListItem() { Text = projt.NAME, Value = projt.ID.ToString() });
          var result = new SelectList(projectslist, "Value", "Text", tm.PROJ_ID);
          return Json(result, JsonRequestBehavior.AllowGet);
      }
  </code></pre>
  
  <p>I have tried, but getting Error like </p>
  
  <pre><code>      "Syntax error, Unrecognized Expression"
  </code></pre>
  
  <p>where I am Doing Wrong , please help me anyone. </p>