Sending a collection of objects to the view in MVC
-
Hello, I am a Very New programmer in the MVC architecture and I am struggling with getting data from my controller to my view. I am in my last year of college and we are building a web application for a non profit organization that will track residents in their facility. I am trying to query the database for the rooms table and select the rooms that have the IsOccupied field set to false. Then I am filtering the returned query for a range of rooms and putting them in Wings and passing them to the view. My struggles are with getting the room object from the controller to the view. Then in the view, burrowing down into the object and getting the room number out and displaying it. Currently I have the following in my class. public class Room { public int RoomID { get; set; } [Display(Name = "Room Number")] public int RoomNum { get; set; } [Display(Name = "Is Occupied")] public bool IsOccupied { get; set; } } My Controller code is: public ActionResult Create() { //Query database for IsOccupied flag// var AvailRoom = from s in db.Rooms .Where(s => s.IsOccupied == false) select s; foreach (var room in AvailRoom) { if (room.RoomNum > 101 && room.RoomNum < 126) { IEnumerable EastSouth = new SelectList(room.RoomNum.ToString()); ViewBag.EastSouth = EastSouth; } } My view code is:
@Html.Label("Available Rooms")
@Html.DropDownList("EastSouth")
Currently all I get when I compile is a dropdown box with 1, 2 and 5. I am stumped. Any recommendations on where to go from here would be really appreciated.
-
Hello, I am a Very New programmer in the MVC architecture and I am struggling with getting data from my controller to my view. I am in my last year of college and we are building a web application for a non profit organization that will track residents in their facility. I am trying to query the database for the rooms table and select the rooms that have the IsOccupied field set to false. Then I am filtering the returned query for a range of rooms and putting them in Wings and passing them to the view. My struggles are with getting the room object from the controller to the view. Then in the view, burrowing down into the object and getting the room number out and displaying it. Currently I have the following in my class. public class Room { public int RoomID { get; set; } [Display(Name = "Room Number")] public int RoomNum { get; set; } [Display(Name = "Is Occupied")] public bool IsOccupied { get; set; } } My Controller code is: public ActionResult Create() { //Query database for IsOccupied flag// var AvailRoom = from s in db.Rooms .Where(s => s.IsOccupied == false) select s; foreach (var room in AvailRoom) { if (room.RoomNum > 101 && room.RoomNum < 126) { IEnumerable EastSouth = new SelectList(room.RoomNum.ToString()); ViewBag.EastSouth = EastSouth; } } My view code is:
@Html.Label("Available Rooms")
@Html.DropDownList("EastSouth")
Currently all I get when I compile is a dropdown box with 1, 2 and 5. I am stumped. Any recommendations on where to go from here would be really appreciated.
You're looping through the rooms, but you're overwriting the
EastSouth
list each time. You're also passing astring
as the only parameter to theSelectList
constructor. This will call the SelectList(IEnumerable)[^] overload, which will create a list item for each character in the string. Change your code to pass the list of rooms to theSelectList
constructor, along with the name of the property which contains the value and the text:var AvailRoom = db.Rooms
.Where(s => s.IsOccupied == false)
.Where(s => s.RoomNum > 101 && s.RoomNum < 126)
;ViewBag.EastSouth = new SelectList(AvailRoom, "RoomNum", "RoomNum");
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You're looping through the rooms, but you're overwriting the
EastSouth
list each time. You're also passing astring
as the only parameter to theSelectList
constructor. This will call the SelectList(IEnumerable)[^] overload, which will create a list item for each character in the string. Change your code to pass the list of rooms to theSelectList
constructor, along with the name of the property which contains the value and the text:var AvailRoom = db.Rooms
.Where(s => s.IsOccupied == false)
.Where(s => s.RoomNum > 101 && s.RoomNum < 126)
;ViewBag.EastSouth = new SelectList(AvailRoom, "RoomNum", "RoomNum");
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer