title: Adding Autocomplete and TagEditor Jquery to MVC
body: <p>I'm looking to combine the AutoComplete and TagEditor features together, drawing the data to be used from a database.  This is going to go on to populate a new entry as an string, but for now I'm just focusing on getting the features working.</p>
  
  <p>So far, I've tried a couple of different approaches, from both the original examples to links here like <a href="https://stackoverflow.com/questions/27609495/jquery-ui-autocomplete-asp-net-mvc5-rendering-as-a-list">jquery-ui-autocomplete-asp-net-mvc5-rendering-as-a-list</a>, but I'm not going around in circles. </p>
  
  <p>I have 2 main issues, and I'm not sure if they are linked or not. </p>
  
  <p>Firstly, The TagEditor feature is working on my site as a standalone field. I set up a partial view as a test with 2 fields in place. The top one is formatting correctly as a Tag TextArea as expected, but the bottom one is where I am trying to link this into a HTML helper for a string field, it doesn't pick up the JQuery element.</p>
  
  <p><img src="https://i.stack.imgur.com/vGrzs.png" alt="TextArea issues"></p>
  
  <p><strong>_SkillSearch.cshtml</strong></p>
  
  <pre><code>@model NR.Models.Critvit
  
  @using (Html.BeginForm())
  {
  @Html.AntiForgeryToken()
  &lt;hr /&gt;
  &lt;div class="form-horizontal"&gt;
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
      &lt;div class="form-horizontal"&gt;
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.KeySkills, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  &lt;textarea class="skills"&gt;&lt;/textarea&gt;
                  @Html.ValidationMessageFor(model =&gt; model.KeySkills, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;br /&gt;
          &lt;hr /&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.KeySkills, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.TextAreaFor(model =&gt; model.KeySkills, new { htmlAttributes = new { id = "skills", @class = "skills form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.KeySkills, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>Secondly, the query doesn't want to use any of the Source values set in the function.  This is the most recent version, where I'm trying to get the list of skills from the db and use them as the Source array's.  I'm not seeing any JSON activity when I run the page and use the fields either.  This is a couple of different ways I've tried. </p>
  
  <p><strong>Script in _Layout.cshtml  - Version 1</strong></p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function () {
      $('#skillslist').tagEditor({
          autocomplete: {
              delay: 0, position: { collision: 'flip' },
              source: function(request, response) {
                  $.ajax({
                      type: "POST",
                      url: "/skills/search",
                      dataType: "json",
                      data: {
                          term: request.term
                      },
                      error: function(xhr, textStatus, errorThrown) {
                          alert('Error: ' + xhr.responseText);
                      },
                      success: function(data) {
                          response($.map(data, function(c) {
                              return {
                                  label: s.SkillName,
                                  value: s.SkillId
                              }
                          }));
                      }
                  });
              },
                  forceLowercase: false,
                  placeholder: 'Skills Titles (placeholder) ...'
              }
          });
  });
  </code></pre>
  
  <p>  </p>
  
  <p><strong>Script in _Layout.cshtml  - Version 2</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function () {
      $('.skills').tagEditor({
          autocomplete: {
              delay: 0, position: { collision: 'flip' },
              source: '@Url.Action("GetSkillList")'},
                  forceLowercase: false,
                  placeholder: 'Skills Titles (placeholder) ...'
          });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Home Controller</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using NR.DAL;
  using NR.Models;
  
  namespace NR.Controllers
  {
  public class HomeController : Controller
  {
      private NRContext db = new NRContext();
  
      public ActionResult Index()
      {
          return View();
      }
  
      [Cut for space]
  
      public ActionResult GetSkillList(string term)
      {
          var result = from s in db.Skills
                       where s.SkillName.ToLower().Contains(term)
                       select s.SkillName;
          return Json(result.ToList(), JsonRequestBehavior.AllowGet);
      }
  }
  </code></pre>
  
  <p>}</p>