Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
4.00/5 (1 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:


Controller:

C#
//
        // GET: /Patient/Create
        [Authorize]
        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

        [HttpPost]
        [Authorize]
        public ActionResult Create(PatientModel patientmodel)
        {
            if (ModelState.IsValid)
            {
                db.Patients.Add(patientmodel);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(patientmodel);
        }




View:

C#
<div class="editor-label">
            @Html.LabelFor(model => model.Salutation_id, "Salutation")
        </div>
        <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>

        <div class="editor-label">
            @Html.LabelFor(model => model.PatientDateOfBirth)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PatientDateOfBirth)
            @Html.ValidationMessageFor(model => model.PatientDateOfBirth)
        </div>


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

1 solution

Try like this
Add a common class for DropDownList like below
C#
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"] =
                DropDownList<executives>.LoadItems(
                    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--")
 
Share this answer
 
v2

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


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