我是MVC和JQUERY的新手。面对很多问题要分析...... 我想为两个文本框实现自动完成。但无法实现它。我无法找到问题来纠正这个问题。甚至没有火力突破点控制器中的方法...请帮助我......!
在视图中
<div id="srh-city">
<div style="margin-right: 16px; float: left; display: inline">
<label>
LEAVING FROM</label>
<div>
@Html.TextBox("txtFrom", "", new { @class = "main-text from", placeholder = "Enter From City", required = "required ", tabindex = 1 })
<br />
</div>
</div>
<div style="float: left">
<label>
GOING TO</label><div>
@Html.TextBox("txtto", "", new { @class = "main-text to", placeholder = "Enter To City", required = "required ", tabindex = 2 })
<br />
</div>
</div>
</div>
在控制器
中 public JsonResult AutoComplete(string qry)
{
Dimensions objDimensions = new Dimensions();
TYSLayer.TYService.City[] cities = objDimensions.GetFromCities();
var items = from p in cities
select p.CityName;
JavaScriptSerializer jss = new JavaScriptSerializer();
var namelist = items.Where(n => n.ToLower().StartsWith(qry.ToLower()));
return Json(namelist, JsonRequestBehavior.AllowGet);
}
JQuery的
$("#txtFrom").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Home/AutoComplete", type: "POST", dataType: "json",
data: { qry: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.label, value: item.label, id: item.id };
}));
},
select: function (event, ui) {
//update the jQuery selector here to your target hidden field
$("#txtFrom").val(ui.item.id);
}
});
},
minLength: 1
});