[英]Binding a AutoComplete type Textbox with selected dropdown lists options in MVC Razor View
Hi I have to make an auto-complete type textbox which on typing any letter searches the database and displays similarly typed combinations. 嗨,我必须制作一个自动完成类型的文本框,在键入任何字母时,它会搜索数据库并显示类似类型的组合。
I was able to make it using jQuery, but now I want to bind the results in the textbox based on the previously selected values from two drop down lists. 我能够使用jQuery做到这一点,但是现在我想根据先前从两个下拉列表中选择的值,将结果绑定到文本框中。 I have a dropdownlist State and another is City .
我有一个下拉列表州 ,另一个是城市 。 Both dropdown lists are cascading.
两个下拉列表都是级联的。 Now the AutoComplete type Textbox should only display results based on the selected StateId and CityId .
现在,“自动完成”类型的“文本框”应仅显示基于所选StateId和CityId的结果 。
View 视图
<div class="col-md-6 col-sm-6 col-xs-12 ">
<label>State<span class="error">*</span></label>
@Html.DropDownList("StateId", (IEnumerable<SelectListItem>)ViewData["StateList"], "--Select--", new { @placeholder = "--Select--", @class = "form-control" })
</div>
<div class="col-md-6 col-sm-6 col-xs-12 ">
<label>City <span class="error">*</span></label>
@Html.DropDownListFor(m => m.CityId, (IEnumerable<SelectListItem>)ViewData["CityList"], "--Select--", new { @placeholder = "--Select--", @class = "form-control" })
</div>
<div class="col-md-12 col-sm-12 col-xs-12 ">
<label>Customer Name <span class="error">*</span></label>
@Html.TextBoxFor(m => m.CustomerName, new { @placeholder = "Enter Customer Name", @class = "required form-control", @maxlength = 100 })
@Html.ValidationMessageFor(model => model.CustomerName, null, new { @style = "color:red;!important" })
</div>
<input type="hidden" id="hfCustomer" name="CustomerId" />
<br /><br />
JavaScript 的JavaScript
<script type="text/javascript">
$(function () {
$("#CustomerName").autocomplete({
source: function (request, response) {
$.ajax({
url: '/Home/AutoComplete/',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data, function (item) {
return item;
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("#hfCustomer").val(i.item.val);
},
minLength: 1
});
});
</script>
Controller 控制者
[HttpPost]
public JsonResult AutoComplete(string prefix)
{
var customers = (from customer in dbContaxt.Cities
where customer.Name.StartsWith(prefix)
select new
{
label = customer.Name,
val = customer.CityId
}).ToList();
return Json(customers);
}
How can I bind both selected StateId and CityId and retrieve CustomerName in the textbox? 如何绑定选定的StateId和CityId并在文本框中检索CustomerName? Please suggest changes in jQuery and controller.
请建议更改jQuery和控制器。 Thanks!
谢谢!
$.ajax({
url: '/Home/AutoComplete/',
data: { data: "{ 'prefix': '" + request.term + "'}", stateId: "slected State Id",cityid:"Selected City ID" },
Controller like below 像下面的控制器
public JsonResult AutoComplete(string prefix,int stateId,int cityid)
{
var customers = (from customer in dbContaxt.Cities
where customer.Name.StartsWith(prefix)&&customer.state==state && customer.city==city
select new
{
label = customer.Name,
val = customer.CityId
}).ToList();
return Json(customers);
}
All you have to do is, read the selected option value of State and City dropdown and send that in the request payload. 您所要做的就是,读取State和City下拉菜单的选定选项值,并将其发送到请求有效负载中。
Also if you are specifying contentType
as application/json
, you should JSON stringify the payload. 另外,如果您将
contentType
指定为application/json
,则应使用JSON对有效负载进行字符串化。 If your payload is a lean flat js object, you do not need to do that. 如果您的有效载荷是一个精简的js对象,则无需这样做。 Simply remove the contentType property.
只需删除contentType属性。
dataType is a property which tells the $.ajax
method the type of data it is expecting from the server. dataType是一个属性,它告诉
$.ajax
方法其从服务器期望的数据类型。 But if not provided, jQuery will intelligently guess it based on the response headers ( Content-Type
header) and use that. 但是,如果未提供,jQuery将根据响应标头(
Content-Type
标头)智能地猜测它并使用它。 In this case, since we are explicitly returning Json from the controller, there will be response header for Content-Type
whic hhas value application/json
. 在这种情况下,由于我们从控制器显式返回了Json,因此将为
Content-Type
响应标头提供值application/json
。 So you can remove that as well from your call. 因此,您也可以将其从通话中删除。
$(function() {
$("#CustomerName").autocomplete({
source: function (request, response) {
var d = { term: request.term,
stateId: $("#StateId").val(),
cityId: $("#City").val() };
$.ajax({
url: '@Url.Action("AutoComplete")',
data: d,
type: "POST",
success: function (data) {
response($.map(data, function (item) {
return item;
}))
},
error: function (response) {
console.log(response.responseText);
},
failure: function (response) {
console.log(response.responseText);
}
});
},
select: function (e, i) {
$("#hfCustomer").val(i.item.val);
},
minLength: 1
});
});
Now add these 2 parameters to your action method and you can use that to filter your data 现在,将这两个参数添加到您的操作方法中,您可以使用它来过滤数据
[HttpPost]
public ActionResult AutoComplete(string term,int countryId,int stateId)
{
// to do use the parameters and filter data and return some JSON
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.