Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MVC grid MVC3 CodeFirst dropdown , +
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
    {
        [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
 
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
 
 
@{
    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
 
        //
        // 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
 
grid.Column(header: "Wards", format: @<text>@Html.DropDownList("Wards")</text>)
 
to
 
@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
Edited 4-Mar-13 9:23am
v5

1 solution

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

Solution 1

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

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2014
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