0
我試圖在特定的文本框上實現自動填充,但目前爲止還沒有工作。這是我的代碼。我錯過了什麼?MVC:Textbox autopopulate無法正常工作
VIEW:
<script type="text/javascript">
$(document).ready(function() {
$("#lead-organisation").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Project/AutoCompleteOrganisation",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.Organisation, value: item.Organisation };
}))
}
})
},
messages: {
noResults: "", results: ""
}
});
})
</script>
<div class="form-group">
<label for="@Html.IdFor(x => x.Organisation)">
@Html.DisplayNameFor(x => x.Organisation)
</label>
@Html.TextBoxFor(x => x.Organisation, new { @class = "form-control", data_project = "organisation", editorId="lead-organisation" })
@Html.ValidationMessageFor(x => x.Organisation)
</div>
我的控制器:
[AjaxOnly]
public ActionResult AutoCompleteOrganisation(string term)
{
var taxonomy = DependencyResolver.Current.GetService<TaxonomyHelper>();
var organisationList = new ProjectOrganisationModel();
organisationList.Organisation = taxonomy.GetOrganisations();
var result = from org in organisationList.Organisation
where org.Name.ToLower().Contains(term.ToLower())
select org.Name;
return Json(result, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet);
}
我也下載了這個:jquery.autocomplete.js並增加它在我的BundleConfig.cs .INCLUDE(「〜/腳本/jquery.autocomplete.js「)
你得到任何控制檯錯誤? –
你沒有帶id =「lead-organization」的元素(但是你有一個id =「Organization」') - 它需要是'$(「#Organization」)。autocomplete({ ...' –
我使用的IE瀏覽器,並沒有錯誤彈出的JS。將使用FireBug嗎? – JADE