Click here to Skip to main content
Rate this: bad
good
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:
 

Controller:
 
//
        // 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:
 
<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 10-Feb-13 3:56am
Edited 10-Feb-13 6:39am
v4
Comments
Jameel Moideen at 10-Feb-13 11:09am
   
what is the problem actually?Please improve your question
Jameel Moideen at 10-Feb-13 11:09am
   
inappropriate values means?
dedo1991 at 10-Feb-13 11:38am
   
updated, i hope it provides more info

1 solution

Rate this: bad
good
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"] =
                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--")
    
        
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2014
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