Hello,
I have the following models: Department (as city), District and Store.
Department has one to many Districts
Districts has one to many Stores
I'm constructing a view where I create a new Store and the user can choose the Department(city) and then the District dropdownlist updates instantly with the corresponding districts.
For this I created this ViewModel: StoreIndexData
- public class StoreIndexData
- {
- public Store Stores { get; set; }
- public Department Departments { get; set; }
- }
This is the Controller where I populate the dropdownlist for the Department:
- public IActionResult Create() {
- //Departaments List
- List
Departmentlist = _context.Departments.ToList(); - Departmentlist.Insert(0, new Department { DepartmentID = 0,
- DepartmentName = "Select" });
- ViewBag.DepartmentList = new SelectList(Departmentlist,
- "DepartmentID", "DepartmentName");
- return View(); }
And this is the View:
- @model Application.Models.ApplicationviewModels.StoreIndexData
- >
- class="form-horizontal">
Store
"ModelOnly" class="text-danger">class="form-group">- ="Departments.DepartmentID" class="col-md-2 control-label">
class="col-md-10">- ="Departments.DepartmentID"
- class="form-control" asp-items="ViewBag.DepartmentList">
class="form-group">- ="Stores.DistrictID" class="col-md-2 control-label">
class="col-md-10">- @Html.DropDownListFor(model => model.Stores.DistrictID, new SelectList(" "), "Select", new { @class = "form-control"})
And in the end of the view, this is my JS section:
This section calls this action:
- public JsonResult GetDistrictList(int DepartmentID)
- {
- List
DistrictList = _context.Districts.Where(x => x.DepartmentID == DepartmentID).ToList(); - return Json(DistrictList);
- }
And that's it.
When I run the application, well, the District dropdownlist don't populate
_Questions:
Is it possible that I can't access this properties inside the JScript
"#DepartmentID"like this since I'm using a viewmodel? Maybe I need to change it to something like "#Departments.DepartmentID"?Could this work using a ViewModel? I'm not adding the Department property inside the Store model because it would be irrelevant since it gets its Department thru the District.
I saw in the example that I followed that they added this line ProxyCreationEnabled = false Inside the JsonResult action. I didn't add it because I don't know what it does and don't know the consecuences in my application if I add it.
Thanks in advance for any help.
Regards,
_Update:
I put a break point on this line, inside the JsonResult:
- List
DistrictList = _context.Districts. - Where(x => x.DepartmentID == DepartmentID).ToList();
But the application did not stopped there when I used the Department dropdownlist that is supossed to call that JsonResult. I believe the script section is what might be wrong.
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Luis Alberto Delgado de la FlorPosted Jun 27, 2017, 10:25 PM
Manikandan MurugesanPosted Jun 27, 2017, 8:34 PM