Click here to Skip to main content
13,400,497 members (37,988 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
Dear programming community,

I am using code first migrations and i am trying to figure out how i can retrieve data from "Many-to-one" relationships. In other words i have a Doctor where one doctor can belong to many wards but ward can have only one doctor at a time.

My objective is to retrieve doctors' wards for a dropdownlist located inside a grid.

Please help me. Any comments or suggestions are welcome.

Doctor Model

public class DoctorModel
        [Display(Name = "Doctor ID:")]
        [Required(ErrorMessage = "Doctor ID is required!")]
        public int Doctor_id { get; set; }

        [Display(Name = "Doctor User Name:")]
        [Required(ErrorMessage = "Doctor User Name is required!")]
        [StringLength(50, ErrorMessage = "Must be under 50 characters!")]
        public string Doctor_username { get; set; }

        [Display(Name = "Doctor Bleep ID:")]
        [Required(ErrorMessage = "Doctor Bleep ID is required!")]
        public int Doctor_bleep_id { get; set; }

        public string Image_name { get; set; }

        public int Image_size { get; set; }

        [Display(Name = "Upload File:")]
        public byte[] Image_bytes { get; set; }

        // one to many relationships
        public virtual ICollection<EquipmentModel> Equipments { get; set; }

        public virtual ICollection<WardModel> Wards { get; set; }

        public int? Gender_id { get; set; }
        public virtual GenderModel Gender { get; set; }

Ward Model

public class WardModel
        [Display(Name = "Ward ID:")]
        public int Ward_id { get; set; }

        [Display(Name = "Ward Name:")]
        [Required(ErrorMessage = "Ward Name is required!")]
        [StringLength(50, ErrorMessage = "Must be under 50 characters!")]
        public string Ward_name { get; set; }

        [Display(Name = "Ward Capacity:")]
        [Required(ErrorMessage = "Ward Capacity is required!")]
        public int Ward_capacity { get; set; }

        [Display(Name = "Ward Speciality:")]
        [Required(ErrorMessage = "Ward Speciality is required!")]
        [StringLength(50, ErrorMessage = "Must be under 50 characters!")]
        public string Ward_speciality { get; set; }

        [Display(Name = "Ward Status:")]
        public string Ward_status { get; set; }

        [Display(Name = "Ward Moto:")]
        [StringLength(50, ErrorMessage = "Must be under 50 characters!")]
        public string Ward_moto { get; set; }

        // many to one relationships
        [Display(Name = "Ward Manager:")]
        public int? Doctor_id { get; set; }
        public virtual DoctorModel Doctor { get; set; }

        // one to many relationships
        public virtual ICollection<PatientModel> Patients { get; set; }

        public virtual ICollection<NurseModel> Nurses { get; set; }


    ViewBag.Title = "Index";

<link rel="stylesheet" href="@Url.Content("~/Content/CoreCSS/GridStyle.css")" />
<script src="@Url.Content("~/Content/Scripts/GenericDetailScripts.js")" type="text/javascript"></script>

@if (Model != null)
    <section class="contentContainer">

        var grid = new WebGrid(Model, canPage: true, rowsPerPage: 15);
        @grid.GetHtml(tableStyle: "listing-border", headerStyle: "k-header myGrid-space", footerStyle: "k-pager-wrap k-grid-pager", rowStyle: "td-dark", alternatingRowStyle: "td-light",
                htmlAttributes: new { id = "DataTable" },
                columns: grid.Columns(
                            grid.Column("Doctor_id", "Doctor ID:", style: "colMin"),
                            grid.Column(header: "Wards", format: @<text>@Html.DropDownList("Wards")</text>)


In my controller

// GET: /Doctor/
public ViewResult Index()
    ViewBag.Wards = new SelectList(db.Wards, "Ward_id", "Ward_name");
    return View(db.Doctors.ToList());


Based on jerrykids' suggestion I changed the following line

grid.Column(header: "Wards", format: @<text>@Html.DropDownList("Wards")</text>)


@Html.DropDownList("Wards", (SelectList)ViewBag.Wards)

The output of that is that now i am getting a list of all the wards for every doctor in the grid.
However, i am interested in retrieveing a list of wards WHERE id of the doctor is item.Doctor_id. -> basicaly retrieve the doctor_id for a particular grid row.
Posted 3-Mar-13 8:59am
Updated 4-Mar-13 9:23am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

@Html.DropDownList("Wards", (SelectList)ViewBag.Wards)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180218.2 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100