I have two DropDownLists. One is for GymLocation
and the other is for RoomNumbers
in that Gym. I want to allow the user to first select from GymLocation
DropDownList and then I want to have the corresponding room numbers in the RoomNumbers
DropDownList. Therefore, The RoomNumbers
DropDownList will update depending on the selected location (data will be queried from the database). I am not sure how I should do it.
This is BranchViewModel:
public class BranchViewModel
{
public IEnumerable<Branch> Branch { get; set; }
public IEnumerable<Room> Rooms { get; set; }
}
This is the controller. Note: I have hard coded a record.
public ActionResult Index()
{
var item = GetBranches(3);
return View(item);
}
Here I am getting a specific Branch and all the Room numbers for that branch.
public BranchViewModel GetBranches(int id)
{
var branch = _context.Branches.Where(b => b.Id == id).ToList();
var rooms = _context.Rooms.Where(b => b.BranchId == id).ToList();
var List = new BranchViewModel
{
Branch = branch,
Rooms = rooms
};
return List;
}
This is the view:
@model YogaFitnessClub.ViewModels.BranchViewModel
<div class="form-group col-lg-4">
@Html.LabelFor(m => m.Branch)
@Html.DropDownListFor(m => m.Branch, new SelectList(Model.Branch, "Id", "LocationName"), "Select From below Room Names", new { @class = "form-control" })
</div>
<div class="form-group col-lg-4">
@Html.LabelFor(m => m.Branch, "Room Numbers")
@Html.DropDownListFor(m => m.Rooms, new SelectList(Model.Rooms, "Id", "RoomNumber"), "Select From below Room Numbers", new { @class = "form-control" })
</div>
Thank you