HendPro12
HendPro12

Reputation: 1104

MVC3 dropdown list that displays mutliple properties per item

I want to have a dropdownlist in my view that displays a patient's ID, First Name, and Last Name. With the code below, it displays each patient's First Name. How can I pass all three properties into the viewbag and have them display in the dropdownlist?

Controller

public ActionResult Create()
            {ViewBag.Patient_ID = new SelectList(db.Patients, "Patient_ID", "First_Name");
             return View();
            } 

View

    <div class="editor-field">
                @Html.DropDownList("Patient_ID", String.Empty)
                @Html.ValidationMessageFor(model => model.Patient_ID)
            </div>

Thanks.

Ok, I have edited my code as follows, and I receive the error "There is no ViewData item of type 'IEnumerable' that has the key 'SelectedPatientId'."

Controller


    public ActionResult Create()
            {
                var model = new MyViewModel();

        {
             var Patients = db.Patients.ToList().Select(p => new SelectListItem
            {
                Value = p.Patient_ID.ToString(),
                Text = string.Format("{0}-{1}-{2}", p.Patient_ID, p.First_Name, p.Last_Name)
            });

            var Prescribers = db.Prescribers.ToList().Select(p => new SelectListItem
    {
        Value = p.DEA_Number.ToString(),
        Text = string.Format("{0}-{1}-{2}", p.DEA_Number, p.First_Name, p.Last_Name)
    });

            var Drugs = db.Drugs.ToList().Select(p => new SelectListItem
            {
                Value = p.NDC.ToString(),
                Text = string.Format("{0}-{1}-{2}", p.NDC, p.Name, p.Price)
            });


        };
                return View(model);
            }

View Model

    public class MyViewModel
        {
            [Required]
            public int? SelectedPatientId { get; set; }
            public IEnumerable<SelectListItem> Patients { get; set; }

            [Required]
            public int? SelectedPrescriber { get; set; }
            public IEnumerable<SelectListItem> Prescribers { get; set; }

            [Required]
            public int? SelectedDrug { get; set; }
            public IEnumerable<SelectListItem> Drugs { get; set; }
        }

View

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

     @Html.DropDownListFor(
        x => x.SelectedPatientId,
        Model.Patients,
        "-- Select patient ---"
    )
    @Html.ValidationMessageFor(x => x.SelectedPatientId)
    <button type="submit">OK</button>

         @Html.DropDownListFor(
        x => x.SelectedPrescriber,
        Model.Patients,
        "-- Select prescriber ---"
    )
    @Html.ValidationMessageFor(x => x.SelectedPrescriber)
    <button type="submit">OK</button>
}

Upvotes: 3

Views: 2121

Answers (2)

Paweł Staniec
Paweł Staniec

Reputation: 3181

Easy way to accomplish that is just creating additional property on your model either by modifying model class or adding/modifying a partial class

[NotMapped]
public string DisplayFormat
{
    get
    {
      return string.Format("{0}-{1}-{2}", Patient_ID, First_Name, Last_Name);
    }  
}

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039060

I would recommend you not to use any ViewBag at all and define a view model:

public class MyViewModel
{
    [Required]
    public int? SelectedPatientId { get; set; }
    public IEnumerable<SelectListItem> Patients { get; set; }
}

and then have your controller action fill and pass this view model to the view:

public ActionResult Create()
{
    var model = new MyViewModel
    {
        Patients = db.Patients.ToList().Select(p => new SelectListItem
        {
            Value = p.Patient_ID.ToString(),
            Text = string.Format("{0}-{1}-{2}", p.Patient_ID, p.First_Name, p.Last_Name)
        });
    };
    return View(model);
} 

and finally in your strongly typed view display the dropdown list:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.DropDownListFor(
        x => x.SelectedPatientId, 
        Model.Patients, 
        "-- Select patient ---"
    )
    @Html.ValidationMessageFor(x => x.SelectedPatientId)
    <button type="submit">OK</button>
}

Upvotes: 1

Related Questions