I have the following JS which I use to populate a dropdownlist (District) depending on which Department the user chose.
Here the code:
- public IActionResult Create(int? id)
- {
- List
DepartmentList = new List (); - DepartmentList = (from department in _context.Departments
- select department).ToList();
- DepartmentList.Insert(0, new Department { DepartmentID = 0, DepartmentName = "Select" });
- ViewBag.ListofDepartment = DepartmentList;
- //...etc
- }
- form asp-action="Create" role="form">
- "ModelOnly" class="text-danger">
- class="modal-body form-horizontal">class="form-group">
- ="DepartmentID" class="col-md-2 control-label">
class="col-md-10">- ="DepartmentID" class="form-control"
- asp-items="@(new SelectList(@ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))">
class="form-group">- ="col-md-2 control-label">District
class="col-md-10">- ="form-control" id="DistrictID" name="DistrictID" asp-for="DistrictID"
- asp-items="@(new SelectList(string.Empty,"DistrictID","DistrictName"))">
Here, I'm setting the District dropdownlist as a Empty list, since I'll populate it with JS.
After that I include the following Js to Populate the District dropdownlist, first with a Select, and then with the values required depending on which Department was chose.
This works fine when I create a new Store.
The problem:
When I want to update an existing Store, I can put the Department value inside the dropdownlist, but the District dropdownlist don't populate with the value which that Store has or the other possible values in case the user wants to update this particular Store district.
I suspect that I must add some functionality to my JS, but I don't really know how.
IDEA: I suspect that I must change the 2nd part of the JavaScript to:
- Add a conditional if I'm seeing a new or an existing store.
- If it is new, do what you are doing now,
- Else, instead of 'Select', put the corresponding District (and show this) and fill the dropdown with the rest of districts corresponding with that Department.
- After this point, if the user change the Department dropdown, do what you have been doing up until now.
Thanks in advance.
EDIT: Additional info
I'm using a ViewModel since I need the properties from several models: Department, District and Store:
- public class StoreIndexData
- {
- public int DepartmentID { get; set; }
- public string DepartmentName { get; set; }
- public int DistrictID { get; set; }
- public string DistrictName { get; set; }
- public int StoreID { get; set; }
- public int StoreChainID { get; set; }
- public string StoreName { get; set; }
- public string StoreAddress { get; set; }
- public int StoreArea { get; set; }
- }
- public JsonResult GetDistrict(int DepartmentID)
- {
- List
DistrictList = new List (); - DistrictList = (from district in _context.Districts
- where district.DepartmentID == DepartmentID
- select district).ToList();
- DistrictList.Insert(0, new District { DistrictID = 0, DistrictName = "Select" });
- return Json(new SelectList(DistrictList, "DistrictID", "DistrictName"));
- }
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 Jul 15, 2017, 12:33 AM
Puneet KankarPosted Jul 15, 2017, 12:09 AM