在我看来,我有;
@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,以便将其发布到控制器?