Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Sending a collection of objects to the view in MVC

Sending a collection of objects to the view in MVC

Scheduled Pinned Locked Moved ASP.NET
databasearchitecturehtmlasp-netcom
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Davstr
    wrote on last edited by
    #1

    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.

    Richard DeemingR 1 Reply Last reply
    0
    • D Davstr

      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.

      Richard DeemingR Online
      Richard DeemingR Online
      Richard Deeming
      wrote on last edited by
      #2

      You're looping through the rooms, but you're overwriting the EastSouth list each time. You're also passing a string as the only parameter to the SelectList 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 the SelectList 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      D 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You're looping through the rooms, but you're overwriting the EastSouth list each time. You're also passing a string as the only parameter to the SelectList 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 the SelectList 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

        D Offline
        D Offline
        Davstr
        wrote on last edited by
        #3

        First off, I would like to apologize for my horrible code!!!! I reread my post and I am really embarrassed. But I guess it takes making a fool out of yourself to get better. Thank you for your help. This worked like a champ.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups