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

Thanks
tech1guyAsked:
Who is Participating?
 
lenordisteCommented:
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
-valueField
-textField
-selectedValue

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"))%>
0
 
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?
0
 
tech1guyAuthor Commented:
It worked now.
0
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.