@Html.DropDownListFor SelectedValue in a MVC Razor View


Posted on : 23-09-2011 | By : Chris | In : MVC

Although ViewBag is often a quick and dirty way to pass values between the controller and the view be careful not to think of it as your friend. In many (most) occasions this approach is just too simplistic to offer the functionality you require.  More often than not you are best implementing a more robust solution. Auto selecting a dropdownlist value in the view is a perfect example.

If you accept the default code Visual Studio (Express 2010) creates for you when you create a controller from a model “with Read Write Actions and Views” you get something like this:

public ViewResult Index()

            var departments = db.Departments.Include("Company");
            return View(departments.ToList());

@Html.DropDownList(“CompanyID”, String.Empty)


As you can see quick (very quick – auto generated in fact) but dirty…

The correct approach is to forget the ViewBag and go for a more robust approach using a ViewModel…


1. Create a View Model (I placed my in a ‘ViewModel’ folder and called it CompanyDepartments.cs)

using System;
using System.Web.Mvc;
using System.Collections.Generic;

namespace BenefitsBenchmarkingTool.ViewModels
    public class CompanyDepartments
        public int SelectedCompanyId { get; set; }
        public IEnumerable<SelectListItem> Companies { get; set; }
        public string DepartmentName { get; set; }
        public string DepartmentEmail { get; set; }
        public int CompanyID { get; set; }

2. Populate the View Model from the Controller - if, like me you have your View Model in it’s own namespace don’t forget to include it in the controller (e.g. using BenefitsBenchmarkingTool.ViewModels;)

public ActionResult Create(int id)
            var companies = db.Companies;

            var model = new CompanyDepartments
                SelectedCompanyId = id,
                Companies = companies.AsEnumerable().Select(x => new SelectListItem
                    Value = x.CompanyID.ToString(),
                    Text = x.CompanyName
            return View(model);


(Interestingly, if I didn’t add ‘.AsEnumerable()’ I got the error LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression)

3. In the View you reference your View Model:

@model BenefitsBenchmarkingTool.ViewModels.CompanyDepartments

Then use the strongly typed DropDownListFor helper:

@Html.DropDownListFor( x => x.SelectedCompanyId, Model.Companies) 
http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/digg_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/furl_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/google_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/twitter_48.png


I cannot express how thankful am I on reading the phrase:
“(Interestingly, if I didn’t add ‘.AsEnumerable()’ I got the error LINQ to Entities does not recognize the method ‘System.String ToString()’ method, and this method cannot be translated into a store expression)”

I searched everywhere and nobody seemed to have this clean approach. Thank you!

Me too. I lost half a day on this error. Thanx man!

Post a comment