Click here to Skip to main content
12,077,357 members (49,638 online)
Rate this:
Please Sign up or sign in to vote.
See more: MVC3 Razor DropDownList
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")

Any comments or suggestions are welcome.
Posted 10-Feb-13 3:56am
Edited 10-Feb-13 6: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
Web02 | 2.8.160212.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2016
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