Html.DropDownList DisplayMemberPath SelectedItem

Posted on 2009-12-27
Last Modified: 2013-11-08
Hello there,

I'm using Visual Studio 2008 with .NET 3.5 SP1, and MVC. In one of my View, I've a DropDownBox named as "Source": "Source"'s DataSource property is binded to "Sources". Here: Sources is DTO which contains SourceId, Name, UpdatedDate, SourceType.

<%= Html.DropDownList("Source",new SelectList((IEnumerable) Model.Sources,"","Name",""))%>

I'm using DisplayMemberPath property to show ONLY "Name" (sourceName) in the dropdownBox.

My problem is HOW to get the selectedValue in the Controller?

I've tried: Source.SelectedItem, So0urce.SelectedValue, SelectedIndex.....but nothing gives us the selected value. Any help will be appreciated!!

Question by:tech1guy
    LVL 11

    Accepted Solution

    Hi tech1guy, I ve not really used MVC but I see you are probably missing something in your SelectList constructor. The parameters are in order:
    - source

    since you are sending an empty string in your second parameter I assume this is the reason you are not getting anything through source.SelectedValue (which is what you need to call).
    Try this:
    <%= Html.DropDownList("Source",new SelectList((IEnumerable) Model.Sources,"SourceId","Name"))%>

    Author Comment

    I tried the solution that you suggested, but still I could NOT retrieve the selected value by user. Could someone please help me?

    Author Closing Comment

    It worked now.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity C# Stream to WorkBook 5 43
    Getting Compilation Error 3 37
    C# Winform panel within a panel 4 31
    C# primary key 9 46
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now