Nested Lists in a View.... Is it possible, or practical?
-
I a a beginner programmer in my senior year of my CIS degree. I have just recently been introduced to the MVC model of web development and I am currently working on a project for a non profit to produce a web application to track their people that check in and check out. My question is that I ma trying to display their available rooms on my create resident page. I have the code for the total rooms done and it will display. (Currently it goes from a range of 101 to 310) However, now I need to separate them into wings. North Wing, South Wing, etc. Is there a way to nest lists? Example, pass the view a list of wings and then display the drop down box of available rooms according to what wing they select? If so, how would that be implemented?
-
I a a beginner programmer in my senior year of my CIS degree. I have just recently been introduced to the MVC model of web development and I am currently working on a project for a non profit to produce a web application to track their people that check in and check out. My question is that I ma trying to display their available rooms on my create resident page. I have the code for the total rooms done and it will display. (Currently it goes from a range of 101 to 310) However, now I need to separate them into wings. North Wing, South Wing, etc. Is there a way to nest lists? Example, pass the view a list of wings and then display the drop down box of available rooms according to what wing they select? If so, how would that be implemented?
Are you absolutely set on having two lists? It can be done, but for a small list, it's much easier to use an <optgroup>[^] to group a single list.
var availableRooms = db.Rooms.Where(s => s.IsOccupied == false).Select(r => new
{
r.RoomNum,
r.WingName,
});ViewBag.Rooms = new SelectList(availableRooms, dataValueField: "RoomNum", dataTextField: "RoomNum", dataGroupField: "WingName", selectedValue: null);
@Html.DropDownList("Rooms")
SelectList Constructor (System.Web.Mvc)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I a a beginner programmer in my senior year of my CIS degree. I have just recently been introduced to the MVC model of web development and I am currently working on a project for a non profit to produce a web application to track their people that check in and check out. My question is that I ma trying to display their available rooms on my create resident page. I have the code for the total rooms done and it will display. (Currently it goes from a range of 101 to 310) However, now I need to separate them into wings. North Wing, South Wing, etc. Is there a way to nest lists? Example, pass the view a list of wings and then display the drop down box of available rooms according to what wing they select? If so, how would that be implemented?
Research "parent-child relationships" and MVC; e.g. https://www.resultdata.com/presenting-a-parentchild-relationship-with-mvc-3/
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
Are you absolutely set on having two lists? It can be done, but for a small list, it's much easier to use an <optgroup>[^] to group a single list.
var availableRooms = db.Rooms.Where(s => s.IsOccupied == false).Select(r => new
{
r.RoomNum,
r.WingName,
});ViewBag.Rooms = new SelectList(availableRooms, dataValueField: "RoomNum", dataTextField: "RoomNum", dataGroupField: "WingName", selectedValue: null);
@Html.DropDownList("Rooms")
SelectList Constructor (System.Web.Mvc)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Are you absolutely set on having two lists? It can be done, but for a small list, it's much easier to use an <optgroup>[^] to group a single list.
var availableRooms = db.Rooms.Where(s => s.IsOccupied == false).Select(r => new
{
r.RoomNum,
r.WingName,
});ViewBag.Rooms = new SelectList(availableRooms, dataValueField: "RoomNum", dataTextField: "RoomNum", dataGroupField: "WingName", selectedValue: null);
@Html.DropDownList("Rooms")
SelectList Constructor (System.Web.Mvc)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Now this brings me to my next question... Now I have a page takes information about a resident and a room number is selected to put him in. But I am struggling with how to combine these data elements. This is my post method: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "LastName,FirstMidName,Birthdate,ServiceBranch,HasPTSD,Note,InVetCourt")] Resident resident, [Bind (Include = "RoomNum")] Room room) { //query rooms table to get the room ID// var rooms = db.Rooms .Where(s => s.RoomNum == room.RoomNum) .Select(s => s.RoomID); //ResidentContext resident = new ResidentContext(); //resident.Rooms.Include(room.RoomID); try { if (ModelState.IsValid) { //using db.Residents.Include(room); db.Residents.Add(resident); db.SaveChanges(); return RedirectToAction("Index"); } } As you can see, I have tried to use the ResidentContext, but I cannot get that to work.... I have tried to query the Room db and get the room.roomID field out and apply it to the Resident.resident.Add(room) but that also will not work. I have tried to initialize the new resident with the parameters bound and then initialize the Room with the parameters bound, but I cannot get that to work either. I'm pretty sure I am missing something pretty simple, but for the life of me I cannot figure it out. The current table layout is Resident table that has a foreign key to the RoomID field and a room table. Can you point me in the right direction? I cannot seem to get anything valuable to read on how to add the roomID to the resident table. (the room already exists)
-
Now this brings me to my next question... Now I have a page takes information about a resident and a room number is selected to put him in. But I am struggling with how to combine these data elements. This is my post method: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "LastName,FirstMidName,Birthdate,ServiceBranch,HasPTSD,Note,InVetCourt")] Resident resident, [Bind (Include = "RoomNum")] Room room) { //query rooms table to get the room ID// var rooms = db.Rooms .Where(s => s.RoomNum == room.RoomNum) .Select(s => s.RoomID); //ResidentContext resident = new ResidentContext(); //resident.Rooms.Include(room.RoomID); try { if (ModelState.IsValid) { //using db.Residents.Include(room); db.Residents.Add(resident); db.SaveChanges(); return RedirectToAction("Index"); } } As you can see, I have tried to use the ResidentContext, but I cannot get that to work.... I have tried to query the Room db and get the room.roomID field out and apply it to the Resident.resident.Add(room) but that also will not work. I have tried to initialize the new resident with the parameters bound and then initialize the Room with the parameters bound, but I cannot get that to work either. I'm pretty sure I am missing something pretty simple, but for the life of me I cannot figure it out. The current table layout is Resident table that has a foreign key to the RoomID field and a room table. Can you point me in the right direction? I cannot seem to get anything valuable to read on how to add the roomID to the resident table. (the room already exists)
I don't see why you'd need two
DbContext
types for the same database. You should just have one, that looks something like this:public class TheContext : DbContext
{
public IDbSet<Room> Rooms => Set<Room>();
public IDbSet<Resident> Residents => Set<Resident>();
}I'm also not entirely convinced by your data structure. How do you know the dates when a resident is occupying a room? What happens if a resident changes rooms? I'd expect to see something more like this:
Room:
Id
RoomNum
WingNameResident:
Id
LastName
FirstMidName
Birthdate
ServiceBranch
HasPTSD
Note
InVetCourtRoomOccupancy:
RoomId
ResidentId
StartDate
EndDate
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I don't see why you'd need two
DbContext
types for the same database. You should just have one, that looks something like this:public class TheContext : DbContext
{
public IDbSet<Room> Rooms => Set<Room>();
public IDbSet<Resident> Residents => Set<Resident>();
}I'm also not entirely convinced by your data structure. How do you know the dates when a resident is occupying a room? What happens if a resident changes rooms? I'd expect to see something more like this:
Room:
Id
RoomNum
WingNameResident:
Id
LastName
FirstMidName
Birthdate
ServiceBranch
HasPTSD
Note
InVetCourtRoomOccupancy:
RoomId
ResidentId
StartDate
EndDate
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I am sorry for my post earlier. I am part of a 3 student development team and the model changed while I was working on it and I had an old version. I have since updated my model and I can get my dropdown to show the rooms that aren't occupied, but when I try to pass the data to the view, I seem to not be sending any data from the database. This is my controller [get] // GET: Residents/Create public ActionResult Create() { //Query database for IsOccupied flag// //Query for EastSouth Wing// var availRoom = db.Rooms .Where(s => s.IsOccupied == false) .Select(r => new { r.RoomNum, r.WingName, }); ViewBag.rooms = new SelectList(availRoom, dataValueField: "RoomNum", dataTextField: "RoomNum", dataGroupField: "WingName", selectedValue: null); return View(); } This is my current [post] create method: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(ResidentIncomeModel residentIncomeModel, [Bind (Include ="RoomNum")] Room rooms) { Resident resident = new Resident { FirstMidName = residentIncomeModel.FirstMidName, LastName = residentIncomeModel.LastName, Birthdate = residentIncomeModel.Birthdate, ServiceBranch = (Models.ServiceType)residentIncomeModel.ServiceBranch, HasPTSD = residentIncomeModel.HasPTSD, InVetCourt = residentIncomeModel.InVetCourt, //RoomID = rooms.RoomID, Note = residentIncomeModel.Note }; Benefit benefit = new Benefit { Resident = resident, DisabilityPercentage = residentIncomeModel.DisabilityPercentage, SSI = residentIncomeModel.SSI, SSDI = residentIncomeModel.SSDI, FoodStamp = residentIncomeModel.FoodStamp, OtherDescription = residentIncomeModel.OtherDescription, Other = residentIncomeModel.Other, TotalBenefitAmount = residentIncomeModel.TotalBenefitAmount }; try { if (ModelState.IsValid)