Я реализовал автозаполнение, и у меня возникли проблемы с меткой и значением в текстовом поле после выбора элемента. Когда я набираю почтовый индекс, я вижу метку в выпадающем списке:
![image](https://i65.photobucket.com/albums/h208/bsirrah/displayvalue.jpg)
but after I select one, instead of the label showing in the text box, the value (which is the ID that needs to be saved to the database) is displayed:
![image](https://i65.photobucket.com/albums/h208/bsirrah/displayvalue2.jpg)
How do I still show the label after it's selected, but then when the form is saved, it passes the ZipCodeID for the field?
Here's my Controller method:
public JsonResult FindZipCode(string term)
{
VetClinicDataContext db = new VetClinicDataContext();
var zipCodes = from c in db.ZipCodes
where c.ZipCodeNum.ToString().StartsWith(term)
select new { value = c.ZipCodeID, label = c.ZipCodeNum};
return this.Json(zipCodes, JsonRequestBehavior.AllowGet);
}
And here's my markup:
$ (документ) .ready (function () {
$ ( "# ZipCodeID"). Автозаполнения ({
source: '<% = Url.Action ("FindZipCode", "Customers")%>',
});
});
РЕДАКТИРОВАТЬ: Вот мой последний рабочий код:
Контроллер:
public JsonResult FindZipCode(string term)
{
VetClinicDataContext db = new VetClinicDataContext();
var zipCodes = from c in db.ZipCodes
where c.ZipCodeNum.ToString().StartsWith(term)
select new { value = c.ZipCodeID, label = c.ZipCodeNum};
return this.Json(zipCodes, JsonRequestBehavior.AllowGet);
}
и разметка:
<script type="text/javascript">
$(document).ready(function() {
$("#ddZipCode").autocomplete({
source: '<%= Url.Action("FindZipCode", "Customers") %>',
select: function(event, ui) {
var zipCodeID = parseInt(ui.item.value, 1);
$("#ddZipCode").val(ui.item.label);
$("#ZipCodeID").val(ui.item.value);
return false;
}
});
});
</script>
<div class="ui-widget"><input type="text" name="ddZipCode" id="ddZipCode" /></div>
<%= Html.Hidden("ZipCodeID")%>