Click here to Skip to main content
15,891,902 members

MVC dropdownlist inside a grid + Code First Migrations

dedo1991 asked:

Open original thread
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.
Tags: C#, MVC (MVC3), Grid, Drop-down, CodeFirst, DropDownList

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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