Hi,
I recently started to use your awesome controls and I am starting to get a hang of it but there is an issue i can't seen to resolve.
I have two dropdownlists, one of countries and the other of cities and what I want is that when i change an item on country dropdownlist ...the datasource of cities dropdownlist should be updated .
I am doing this using ajax but it still seems to show no records.
View:
@{
ViewData["Title"] = "Index";
}
@using WeatherApp.Controllers
<h2>Index</h2>
<div class="control-wrapper">
<div id="default" style='padding-top:75px;'>
<ejs-dropdownlist id="Countries" dataSource="@ViewBag.Countries" change="onChange" placeholder="Select a Country" popupHeight="220px">
<e-dropdownlist-fields text="Name" value="Code"></e-dropdownlist-fields>
</ejs-dropdownlist>
</div>
</div>
<div class="control-wrapper">
<div id="default" style='padding-top:75px;'>
<ejs-dropdownlist id="Cities" placeholder="Select a City" popupHeight="220px">
<e-dropdownlist-fields text="name" value="id"></e-dropdownlist-fields>
</ejs-dropdownlist>
</div>
</div>
<p id='event'> </p>
<script>
function onChange(args) {
$.ajax({
type: "GET",
url: '@Url.Action("CountrySelected")',
data: { code: args.value },
contentType: "application/json;charset=utf-8",
dataType:"json",
success: function (result) {
debugger;
var cityDropDown = document.getElementById("Cities").ej2_instances[0];
cityDropDown.datasource = result;
cityDropDown.refresh();
alert(result);
},
error: function () {
alert("Error while loading data");
}
});
}
</script>
Controller:
public class WeatherController : Controller
{
private string m_stWebRoot;
public WeatherController(IHostingEnvironment env)
{
m_stWebRoot = env.WebRootPath;
}
public IActionResult Index()
{
string stCountries = System.IO.File.ReadAllText(m_stWebRoot + "\\CustomStaticFiles\\country.list.json");
List<Country> lstCountries = JsonConvert.DeserializeObject<List<Country>>(stCountries);
ViewBag.Countries = lstCountries.OrderBy(o => o.Name).ToList();
return View();
}
public ActionResult CountrySelected(string code)
{
string stCities = System.IO.File.ReadAllText(m_stWebRoot + "\\CustomStaticFiles\\city.list.json");
List<City> allCities = JsonConvert.DeserializeObject<List<City>>(stCities);
allCities = allCities.Where(c => c.CountryCode == code).OrderBy(o => o.Name).ToList();
return Json(allCities);
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
Will appreciate your help in this, Thanks.