Click here to Skip to main content
15,896,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i have a project where by i'm using repository pattern and I'm trying to return information from two table in the form of dropdownlist. This is what my DAL looks like:
C#
namespace DutClinicData
{
    public class AddStudentDoctor
    {
        [Key]
        [Required]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Student Number")]
        public int StudentNumber { get; set; }

        [Required]
        public int Year { get; set; }

        [Required]
        [RegularExpression(@"^[0-9a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Special characters are NOT allowed")]
        [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
        public string Name { get; set; }
}



namespace DutClinicData
{
    public class Room
    {
        [Key]
        public int RoomCode { get; set; }

        public string RoomNumber { get; set; }
        public string RoomLocation { get; set; }
        public virtual AddStudentDoctor AddStudentDoctors { get; set; }
    }
}

now I'm stuck trying to figure out how to return that to my own table that I'll call Allocate_room. in this application i want a person to choose a room from room drop-down and a student doctor from its drop-down then save that info
Posted
Updated 30-Jul-14 1:54am
v2
Comments
Nathan Minier 30-Jul-14 8:46am    
If you're using MVC this would be best accomplished with a ViewModel pushed out from your controller.

If you're using ASP.NET then you just need to assign datasources to your DropDowns.
jasonHall 30-Jul-14 11:17am    
Yeah, so you have the class structures. Great! Now, where is the rest of your code? Where are you creating an instance of these classes & filling them with data?

1 solution

Considering your question do you want to just select the student doctor when creating a room?

if so im not sure if this will help but you can try this:

In your controller:

SomeContext db = new SomeContext();

public ActionResult Index()
{
ViewBag.studentID = new SelectList(db.AddStudentDoctors, "Id", "Name");
ViewBag.roomID = new SelectList(db.Room, "RoomCode", "RoomNumber");
return View();
}

[HttpPost]
public ActionResult Index(Model AllocateRoom, int studentId, int roomCode)
{
//do some code for adding
//something like
ViewBag.studentID = new SelectList(db.AddStudentDoctors, "Id", "Name", studentId);
ViewBag.roomID = new SelectList(db.Room, "RoomCode", "RoomNumber", roomCode);

return View(AllocateRoom);
}

The above code will populate the names of students using their Id's in the AddStudentDoctors
Once its populated you can take the name and add that to your room table

In your view:

@model AllocateRoom

//layout etc

//add your textbox

//add your dropdown list


@Html.DropDownList("studentID", "Choose Doctor")
@Html.ValidationMessageFor(model => model.studentDoctor)



@Html.DropDownList("roomID", "Choose Room")
@Html.ValidationMessageFor(model => model.room)


important viewbag.studentID - studentID is the 1st parameter for your dropdownlist method same for roomID

If this does not help im sorry for not understanding your question
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900