Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Autocomplete and POST Method in Controller
    primarykey
    data
    text
    <p>I got a MVC Razor View page as a form for people to enter details and submit to a database. It works using dropdownlist but I want to add a autocomplete textbox to the form. This works fine however when posting back to the POST controller it sets the ID of the person to 0 always. Any ideas?</p> <p>Thanks</p> <p>Javascript:</p> <pre><code> script type="text/javascript"&gt; $(document).ready(function () { $("#PersonName").autocomplete({ source: '@Url.Action("AutocompleteSuggestions")', minLength: 2, select: function (event, ui) { if (ui.item) { $("#PersonName").val(ui.item.label); $("#PersonId").val(ui.item.value); } } }); }); &lt;/script&gt; </code></pre> <p>Controller:</p> <pre><code> [HttpPost] public ActionResult Create(MappingModel view) { if (ModelState.IsValid) { var entity = new PersonContempancy(); entity.PersonId = view.PersonId; entity.FrameworkId = view.FrameworkId; entity.ContempancyCategoryId = view.ContempancyCategoryId; entity.ContempancyId = view.ContempancyId; entity.ContempancyLevelId = view.ContempancyLevelId; entity.FrameworkLevelId = view.FrameworkLevelId; db.PersonContempancies.Add(entity); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.Contepancies = db.Contempancies.ToList(); ViewBag.ContempancyCategory = db.ContempancyCategories.ToList(); ViewBag.ContempancyLevel = db.ContempancyLevels.ToList(); ViewBag.FrameworkLevel = db.FrameworkLevels.ToList(); ViewBag.Person = db.People.ToList(); ViewBag.Framework = db.Frameworks.ToList(); return View(); } public ActionResult AutocompleteSuggestions(string term) { var namelist = db.People.Where(c =&gt; c.PersonName.Contains(term)).Select(c =&gt; new { value = c.PersonId, label = c.PersonName}).Distinct().Take(10); return Json(namelist.ToArray(), JsonRequestBehavior.AllowGet); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload