Click here to Skip to main content
12,999,417 members (55,895 online)
Rate this:
Please Sign up or sign in to vote.
Dear programming community,

I have the following problem when trying to retrieve a list of salutations from the database/model and putting them in my DropDownListFor control.

I am currently using a viewbag to get the list of items but instead of items with proper names i get items with no values. Each item retrieved from the database by a viewbag is simply represented as "System.Web.Mvc.SelectListItem" in my dropdown list. Please see the code below:


        // GET: /Patient/Create
        public ActionResult Create()
            ViewBag.Patient_ward_id = new SelectList(db.Wards, "Ward_id", "Ward_name");
            ViewBag.Patient_salutation_id = new SelectList(db.Salutations, "Salutation_id");
            return View();
        // POST: /Patient/Create
        public ActionResult Create(PatientModel patientmodel)
            if (ModelState.IsValid)
                return RedirectToAction("Index");  
            return View(patientmodel);


<div class="editor-label">
            @Html.LabelFor(model => model.Salutation_id, "Salutation")
        <div class="editor-field">
       @* @Html.DropDownList("Patient_salutation_id")*@
           @* @Html.DropDownListFor(model => model.Salutation_id, new SelectList(Model.Salutation, "Salutation_id", "Salutation_name"))*@
           @Html.DropDownListFor(model => model.Salutation_id, new SelectList(ViewBag.Patient_salutation_id, "Salutation_id"))
            @Html.ValidationMessageFor(model => model.Salutation_id)
        <div class="editor-label">
            @Html.LabelFor(model => model.PatientDateOfBirth)
        <div class="editor-field">
            @Html.EditorFor(model => model.PatientDateOfBirth)
            @Html.ValidationMessageFor(model => model.PatientDateOfBirth)

Any comments or suggestions are welcome.
Posted 10-Feb-13 2:56am
Updated 10-Feb-13 5:39am
Jameel Moideen 10-Feb-13 11:09am
what is the problem actually?Please improve your question
Jameel Moideen 10-Feb-13 11:09am
inappropriate values means?
dedo1991 10-Feb-13 11:38am
updated, i hope it provides more info

1 solution

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

Solution 1

Try like this
Add a common class for DropDownList like below
public static class DropDownList<t>
       public static SelectList LoadItems(IList<t> collection, string value, string text)
           return new SelectList(collection, value, text);
Call the method from the controller like Below.
ViewData["Executives"] =
                    objExecutivesDbContext.Executives.ToList(), "ExecutiveId", "ExecutiveName ");
Call the viewData from the View like below
<div class="editor-field">
            @Html.DropDownListFor(model => model.ExecutiveId, (IEnumerable<selectlistitem>) ViewData["Executives"], "--Select--")

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2017
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