0
在我看來,我有;MVC jQuery的自動完成,我如何拿起選定的ID?
@using (Ajax.BeginForm("AddExistingSite", new AjaxOptions { UpdateTargetId = "siteRows" }))
{
<input type="text" name="q" style="width: 800px"
data-autocomplete="@Url.Action("SiteSearch", "DataService", new { contractId = @Model.Contract.ContractId })" />
<input type="submit" value="Add site to contract" />
}
在我的控制器我有
public ActionResult SiteSearch(string term, int contractId)
{
using (var db = new SherryGreenGroupEntities())
{
var sites = db.Sites
.Include("SiteContracts")
.Where(x => x.SiteContracts.All(y => y.ContractId != contractId || y.EndDate.HasValue) &&
x.Address.Contains(term))
.Take(10)
.Select(x => new { id = x.SiteId, label = x.Address }).ToList();
return this.Json(sites, JsonRequestBehavior.AllowGet);
}
}
我已經建立了jQuery的;
$(":input[data-autocomplete]").each(function() {
$(this).autocomplete({ source: $(this).attr("data-autocomplete") });
});
現在自動完成工作,但我想知道的是我該怎麼辦拿起所選項目的ID,這樣我可以把它發佈到控制器?