如何在MVC中级联自动完成扩展器? [英] how to Cascade Autocomplete Extender in MVC ?
问题描述
场景:我将通过AutocompleteExtender选择一个城市。关于城市的选择我希望州,地区和国家自动填写。
.cshtmlCode:
Scenario: I will select a City through a AutocompleteExtender. On the selection of City i want State, Region and Country to be filled automatically.
.cshtmlCode:
@Html.LabelFor(m => m.BranchDetails.City)
<div>@Html.EditorFor(model => model.BranchDetails.City, new { htmlAttributes = new { @class = "form-control", style="z-index:999999" }, id = string.Format("txtCity") })
@Html.HiddenFor(model => model.BranchDetails.City, new { id = string.Format("hdfCity") })
脚本:
Script:
<script type="text/javascript">
$("#BranchDetails_City").autocomplete({
source: function (request, response) {
var city = new Array();
$.ajax({
async: false,
cache: false,
type: "POST",
url: 'AutoCompleteCity',
data: { "term": request.term },
success: function (data) {
for (var i = 0; i < data.length ; i++) {
city[i] = { label: data[i].Value, Id: data[i].Key };
}
}
});
response(city);
},
select: function (event, ui) {
$("#hdfCity").html(ui.item.Id)
$("#hdfCity").val(ui.item.Id)
}
});
</script>
控制器代码:
Controller Code:
var listResult = new List<KeyValuePair<string, string>>();
var listResultState = new List<KeyValuePair<string, string>>();
var listResultRegion = new List<KeyValuePair<string, string>>();
var listResultCountry = new List<KeyValuePair<string, string>>();
foreach (var item in obj)
{
listResult.Add(new KeyValuePair<string, string>(item.CityID.ToString(), item.City));
listResultState.Add(new KeyValuePair<string, string>(item.StateID.ToString(), item.State));
listResultRegion.Add(new KeyValuePair<string, string>(item.RegionID.ToString(), item.Region));
listResultCountry.Add(new KeyValuePair<string, string>(item.CountryID.ToString(), item.Country));
}
var finalResult = listResult.Where(s => s.Value.ToLower().Contains
(term.ToLower())).Select(w => w).ToList();
return Json(finalResult, JsonRequestBehavior.AllowGet);
这里我能够在他们的resp列表< keyvaluepair>中获得StateID,State,Region,RegionID,Country,CountryID的值。如上所述。
我无法从这里继续前进。我想将上述值传递给View n分别显示States,Regions,Country。
帮助。
Here i am able to get the Values of StateID,State,Region,RegionID,Country,CountryID in their resp List<keyvaluepair> as above.
I am not able to go any further from here. I want to pass the above values to View n display the States,Regions,Country respectively.
Help.
推荐答案
( #BranchDetails_City)。autocomplete({
source: function (请求,响应){
var city = new Array ();
("#BranchDetails_City").autocomplete({ source: function (request, response) { var city = new Array();
.ajax({
async: false ,
cache: false ,
type: POST,
url:' AutoCompleteCity',
数据:{ term:request.term},
成功: function (数据){
for ( var i = 0 ;我< data.length; i ++){
city [i] = {label:data [i] .Value,Id:data [i] .Key};
}
}
});
回复(城市);
},
select: function (event,ui){
.ajax({ async: false, cache: false, type: "POST", url: 'AutoCompleteCity', data: { "term": request.term }, success: function (data) { for (var i = 0; i < data.length ; i++) { city[i] = { label: data[i].Value, Id: data[i].Key }; } } }); response(city); }, select: function (event, ui) {
( #hdfCity)。html(ui.item.Id)
("#hdfCity").html(ui.item.Id)
这篇关于如何在MVC中级联自动完成扩展器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!