title: Filling Select2 Dropdown using ASP.net MVC4
body: <p>I am trying to fill a dropdown which use Select2 Jquery add-on using asp.net mvc4. I have already search the stackoverflow and found the related topic as below ;</p>
  
  <p><a href="https://stackoverflow.com/questions/14853643/fill-select2-dropdown-box-from-database-in-mvc-4">Fill Select2 dropdown box from database in MVC 4</a></p>
  
  <p>But in my project i cant get this work. </p>
  
  <p><strong>In my View</strong></p>
  
  <pre><code>&lt;div class="control-group"&gt;
                              &lt;label class="control-label"&gt;Proje Adı&lt;/label&gt;
                              &lt;div class="controls"&gt;
                                   &lt;select id="PROJECTID"&gt;
  
                                       &lt;/select&gt;
                                   &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><code>In my controlle</code>r i wrote my action which returns the JSON and i have tested this action with manuel entries and <strong>it is OK</strong>. I can see the JSON result. </p>
  
  <pre><code>  public JsonResult FetchItems(string query)
          {
              List&lt;CRM_PROJECTS&gt; projectList = db.CRM_PROJECTS.Where(p =&gt; p.IS_VALID == 1 &amp;&amp; p.NAME != null).ToList(); //fetch list of items from db table
              List&lt;CRM_PROJECTS&gt; resultProjectList = new List&lt;CRM_PROJECTS&gt;(); //create empty results list
              foreach (var item in projectList)
              {
                  //if any item contains the query string
                  if (item.NAME.IndexOf(query, StringComparison.OrdinalIgnoreCase) &gt;= 0)
                  {
                      resultProjectList.Add(item); //then add item to the results list
                  }
              }
              //resultProjectList.Sort(delegate(Item c1, Item c2) { return c1.ItemName.CompareTo(c2.ItemName); }); //sort the results list alphabetically by ItemName
              var serialisedJsonProjects = from result in resultProjectList //serialise the results list into json
                  select new
                  {
                      name = result.NAME, //each json object will have 
                      id = result.ID      //these two variables [name, id]
                  };
              return Json(serialisedJsonProjects, JsonRequestBehavior.AllowGet); //return the serialised results list
          }
  </code></pre>
  
  <p>In the select2.js file i have implemented the Jquery part of code.</p>
  
  <pre><code> $(document).ready(function () {
          $("#PROJECTID").select2({
  
              placeholder: "Type to find a Contract",
              allowClear: true,
              minimumInputLength: 2,
              ajax:{ 
                  cache: false,
                  dataType: "json",
                  type: "GET",
                  url: "/AddNewOpp/FetchItems",               
                  data: function (searchTerm) {
                      return { query: searchTerm };
                  },
                  results: function (data) { 
                      return {results: data}; 
                  }
              },
              escapeMarkup: function (m) { return m; }
          });
  
   });
  </code></pre>
  
  <p>I could not populate my drop-down and it seems i am unable to call my url:"/AddNewOpp/FetchItems" in the ajax part of the jquery . </p>