Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
@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:
HTML
<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:

C#
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);




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.
Posted
Updated 12-Jan-16 23:15pm
v3
Comments
Sinisa Hajnal 13-Jan-16 6:05am    
Put them in viewbag/session or your views data model.
Abrar Kazi 13-Jan-16 6:44am    
No thats not wat i exactly was looking for. Thanks.

1 solution

OK i got the Solution.

C#
public JsonResult AutoCompleteCity(string term)
       {
           var obj = obj_db.Database.SqlQuery<Branch>("exec spGetCity @term={0}", term).
               Select(x => new Branch {CityID=x.CityID,City=x.City,State=x.State,StateID=x.StateID,Region=x.Region,RegionID=x.RegionID,Country=x.Country,CountryID=x.CountryID }).ToList();
           var listResult = new List<KeyValuePair<string, string>>();

           foreach (var item in obj)
           {
               listResult.Add(new KeyValuePair<string, string>(item.CityID.ToString(), item.City));
           }
           var finalResult = listResult.Where(s => s.Value.ToLower().Contains
                         (term.ToLower())).Select(w => w).ToList();
           return Json(finalResult, JsonRequestBehavior.AllowGet);
       }


       public JsonResult AutoCompleteState(string term)
       {
           var obj = obj_db.Database.SqlQuery<Branch>("exec spGetState @CityID={0}", Convert.ToInt32(term)).
               Select(x => new Branch { CityID = x.CityID, City = x.City, State = x.State, StateID = x.StateID, Region = x.Region, RegionID = x.RegionID, Country = x.Country, CountryID = x.CountryID }).ToList();

           var listResultExtra = new List<KeyValuePair<string, string>>();
           var listResultRegion = new List<KeyValuePair<string, string>>();
           var listResultCountry = new List<KeyValuePair<string, string>>();
           foreach (var item in obj)
           {
               listResultExtra.Add(new KeyValuePair<string, string>(item.StateID.ToString(), item.State));
               listResultExtra.Add(new KeyValuePair<string, string>(item.RegionID.ToString(), item.Region));
               listResultExtra.Add(new KeyValuePair<string, string>(item.CountryID.ToString(), item.Country));
           }
           var finalResult = listResultExtra.Select(w => w).ToList();
           return Json(finalResult, JsonRequestBehavior.AllowGet);
       }


And the Script goes as:

HTML
<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) {
               $.ajax({
                   async: false,
                   cache: false,
                   type: "POST",
                   url: 'AutoCompleteState',
                   data: { "term": ui.item.Id },
                   success: function (data) {

                       $("#BranchDetails_State").val(data[0].Value);
                       $("#hdfState").val(data[0].Key);
                       $("#BranchDetails_Region").val(data[1].Value);
                       $("#hdfRegion").val(data[1].Key);
                       $("#BranchDetails_Country").val(data[2].Value);
                       $("#hdfCountry").val(data[2].Key);
                   }
               });
           }
       });

   </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900