【发布时间】:2016-10-01 19:49:15
【问题描述】:
您好,我的视野中有一个领域。该字段是客户,它是一个下拉字段。因为我将下拉菜单作为选择选项来选择值。但我喜欢将该字段更改为自动完成下拉菜单。
在上图中,我将 customerName 字段作为下拉字段,但我通过搜索和选择选项保留它。但现在我想将其更改为自动完成下拉菜单,如下图所示。
我的查看代码
@Html.Label("Customer Name", new { @class = "control-label" })
@Html.DropDownListFor(model => model.CustomerID, new SelectList(string.Empty, "Value", "Text"), "Please select a Customer", new { @class = "form-control required", type = "text" })
我的 jquery 代码
$(function () {
debugger;
$.ajax(
'@Url.Action("GetVisitCustomer", "VisitorsForm", new { Area = "Sales" })',{
type: "GET",
datatype: "Json",
success: function (data) {
$.each(data, function (index, value) {
$('#CustomerID').append('<option value="' + value.CustomerID + '">' + value.DisplayName + '</option>');
});
}
});
});
我的控制器代码用于获取客户并在现场加载
public JsonResult GetVisitCustomer()
{
var objCustomerlist = (from c in db.Customers where c.IsDeleted== false select c).ToList();
return Json( objCustomerlist,JsonRequestBehavior.AllowGet);
}
我试图解释我的问题。任何人都可以帮助解决此问题。我尝试了很多方法,但它不起作用。所以任何人都理解我的问题并给出一些解决方案或建议。
我尝试过的代码
我的查看代码
@Html.Label("Customer Name", new { @class = "control-label" })
@Html.TextBoxFor(Model=>Model.CustomerID)
我的 Jquery 代码
<script type="text/javascript">
$(document).ready(function () {
debugger;
$("#CustomerID").autocomplete({
source: function (request, response) {
$.ajax(
'@Url.Action("GetVisitCustomer", "VisitorsForm", new { Area = "Sales" })', {
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return
{ label=item.CustomerID, value= item.DisplayName
};
}))
}
})
},
messages: {
noResults: "", results: ""
}
});
})
</script>
但是这段代码不起作用
提前谢谢..
【问题讨论】:
-
你现在的代码有什么问题?
-
我将发布我尝试过但无法正常工作的代码
-
现在检查我尝试过的代码 stephen
-
嗨@Susan,如果我回答了您的问题,您能否标记我的回答。谢谢
标签: c# jquery ajax asp.net-mvc-4 autocomplete