Click here to Skip to main content
11,710,423 members (82,202 online)
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>


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 7:59am
Edited 4-Mar-13 8: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 Sergey Alexandrovich Kryukov 524
1 OriginalGriff 245
2 Peter Leow 194
3 Abhinav S 102
4 Suvendu Shekhar Giri 95
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,037
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2015
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