Autocompletion dans MVC 5
Bonjour,
J'ai utilisé ce bout de code (que je ne comprends pas, au passage)
et voici ce j'en ai fait :
JS :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $(function () {
$("#LocationID").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("GetLocationIDAutocomplet", "Product")',
type: "POST",
dataType: "json",
data: { term: request.term },
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data, function (item) {
return { label: item.LocationZone, value: item.LocationID };
}))
}
})
},
messages: {
noResults: "", results: ""
}
});
}); |
View :
Code:
1 2 3 4 5 6 7 8 9
| <div class="form-group">
<div class="col-md-4">
@Html.LabelFor(model => model.LocationID, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.LocationID, "", new { @class = "text-danger" })
</div>
<div class="col-md-4">
@Html.EditorFor(model => model.LocationID, new { htmlAttributes = new { @class = "form-control", @id = "LocationID" } })
</div>
</div> |
Controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| [Authorize, HttpPost]
public JsonResult GetLocationIDAutocomplet(string term)
{
var user = db.Employees.Where(e => e.Username == User.Identity.Name).SingleOrDefault();
var data = db.Locations
.Include(l => l.Area)
.Where(l => l.IsActiv && l.Area.SiteID == user.SiteId);
if (!string.IsNullOrWhiteSpace(term))
data = data.Where(l => l.LocationID.ToUpper().Contains(term.ToUpper()));
#if DEBUG
System.Diagnostics.Debug.WriteLine(data.ToString());
#endif
return Json(data, JsonRequestBehavior.AllowGet);
} |
Mon appel se passe bien et mon controller répond. Seulement rien ne s'affiche dans ma txtbox :bug:
P.S.: mon input se trouve dans une partial view. Le script dans la view appelante.