Click here to Skip to main content
15,885,918 members
Please Sign up or sign in to vote.
4.00/5 (1 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

C#
public class DoctorModel
    {
        [Key]
        [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

C#
public class WardModel
    {
        [Key]
        [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; }
    }




View

Razor
@{
    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.Pager(WebGridPagerModes.NextPrevious);
        @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>)
                             ));
    }

    </section>
}



In my controller

C#
//
// GET: /Doctor/
[Authorize]
public ViewResult Index()
{
    ViewBag.Wards = new SelectList(db.Wards, "Ward_id", "Ward_name");
    return View(db.Doctors.ToList());
}



=====================================================================================
Updated
=====================================================================================

Based on jerrykids' suggestion I changed the following line

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


to

C#
@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
Updated 4-Mar-13 8:23am
v5

1 solution

Try
C#
@Html.DropDownList("Wards", (SelectList)ViewBag.Wards)
 
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