Html.DropDownList DisplayMemberPath SelectedItem

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!!

Who is Participating?
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"))%>
tech1guyAuthor Commented:
I tried the solution that you suggested, but still I could NOT retrieve the selected value by user. Could someone please help me?
tech1guyAuthor Commented:
It worked now.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.