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. General Programming
  3. Visual Studio
  4. Nested Lists in a View.... Is it possible, or practical?

Nested Lists in a View.... Is it possible, or practical?

Scheduled Pinned Locked Moved Visual Studio
questionasp-netarchitecturetutoriallearning
7 Posts 3 Posters 3 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

    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?

    Richard DeemingR L 2 Replies Last reply
    0
    • D Davstr

      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?

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

      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

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

      D 2 Replies Last reply
      0
      • D Davstr

        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?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          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

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

          Thank you!!! that helped a ton!!

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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

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

            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)

            Richard DeemingR 1 Reply Last reply
            0
            • D Davstr

              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)

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              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
              WingName

              Resident:
              Id
              LastName
              FirstMidName
              Birthdate
              ServiceBranch
              HasPTSD
              Note
              InVetCourt

              RoomOccupancy:
              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

              "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

                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
                WingName

                Resident:
                Id
                LastName
                FirstMidName
                Birthdate
                ServiceBranch
                HasPTSD
                Note
                InVetCourt

                RoomOccupancy:
                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

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

                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)

                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