HowTo: apply MVC2 & Html.DropDownListFor

 

First of all: Merry Christmas to all of you out there and to your family Smiley

image

In an ASP.NET MVC framework you will find a lot of nice HTML Helper. Even one which will help you building a simple HTML <select>. But how does the DropDownListFor helper work?

 

Mhh.. WTF?

To say the true: It takes a while to understand this intellisense for me as well. Maybe it’s because Im not such a smart-ace Zwinkerndes Smiley

image

After spending some time on google queries I found this solution on Stackoverflow.

The Model:

public class SettingsViewModel
{
    public string TimeZone { get; set; }

    public IEnumerable<SelectListItem> TimeZones
    {
        get
        {
            return TimeZoneInfo
                .GetSystemTimeZones()
                .Select(t => new SelectListItem
                {
                    Text = t.DisplayName, Value = t.Id
                });
        }
    }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new SettingsViewModel());
    }

    [HttpPost]
    public ActionResult Index(SettingsViewModel model)
    {
        return View(model);
    }
}

Noting special, isn’t it?

The View:

<% using (Html.BeginForm()) { %>
    <%= Html.DropDownListFor(
        x => x.TimeZone,
        Model.TimeZones,
        new { @class = "SecureDropDown" }
    ) %>
    <input type="submit" value="Select timezone" />
<% } %>

<div><%= Html.Encode(Model.TimeZone) %></div>

First we need to declare where the “selected” element should be mapped → on TimeZone. Second we have the list with the probably results. In the last step you are able to pass some Html attributes.

Result:

image

Fascinating!

[Download Democode]

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Learn more about our team.

4 Responses

  1. Hi
    the article is good and helped me a lot, however in the place of controller code you have copied the view code. check this.

    Reply
  2. @satish: thanks a lot for your help. I fixed it now. Greetings Antje

    Reply
  3. Hi,
    could you prepare such an example with a SELECTED value in in the drop-down ? I have such a case in which i want to have one value selected but i don´t get it working
    Thanks

    Reply
  4. Selected Item
    list.Add(new SelectListItem { Selected = (selected ==sLine), Text = sLine, Value = sLine});

    Reply

Comment on this post

Recent Posts

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

  • image1471-523x194.png
    HTML 5 Games, Tooling & 3D

      Game Developing is an interesting subject for all kind of software developer. But as a web developer without any Flash-skills there aren’t that much starting points. With HTML5 and the combination between Javascript, CSS3 and fast browsers there are the first “robust” HTML5 games. HTML5 games? Is this real? Neowin created a “Top 10” ...

Support us