Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MVC DropDownListFor SelectList Default Selected Value Issues?

Posted on 2014-07-23
2
Medium Priority
?
5,053 Views
Last Modified: 2014-07-25
Very strange, this DropDownListFor looks correct on page load, displays 'This Year'. Problem becomes apparent after clicking the dropdown arrow to displays this:

                    { ID = "1", Name = "Today"}
                    { ID = "2", Name = "This week"}
                    { ID = "3", Name = "This month"}
                    { ID = "4", Name = "This year"}
                    { ID = "5", Name = "All time"}

Open in new window


[View Page Source]
 <div>
<select class="form-control" id="ddlDate" name="date"><option value="">This Year</option>
<option value="">{ ID = 1, Name = Today }</option>
<option value="">{ ID = 2, Name = This week }</option>
<option value="">{ ID = 3, Name = This month }</option>
<option value="">{ ID = 4, Name = This year }</option>
<option value="">{ ID = 5, Name = All time }</option>
</select>
</div>

Open in new window


The DropDownListFor worked great until I added the 'selectedValue' pararm like so:

   @Html.DropDownListFor(model=> model.date, new SelectList(Model.dpSearchList, "Value", "Text", 4), "This Year", new { id = "ddlDate", @class = "form-control" })

Here's the hard-coded SelectList:
dpSearchList = new SelectList(new[]{ 
                new{ ID = "1", Name = "Today"},
                new{ ID = "2", Name = "This week"},
                new{ ID = "3", Name = "This month"},
                new{ ID = "4", Name = "This year"},
                new{ ID = "5", Name = "All time"}}); 

Open in new window

0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 1600 total points
ID: 40215766
Define your SelectList with the selected value:
 SelectList dp = new SelectList(new[]{ 
                new{ ID = "1", Name = "Today"},
                new{ ID = "2", Name = "This week"},
                new{ ID = "3", Name = "This month"},
                new{ ID = "4", Name = "This year"},
                new{ ID = "5", Name = "All time"}},
                "ID", "Name", "4");

Open in new window


Then just access the SelectList from the model in the DropDownFor call.
@Html.DropDownListFor(model => model.date, Model.dpSearchList(),  new { id = "ddlDate", @class = "form-control" } )

Open in new window

0
 

Author Closing Comment

by:WorknHardr
ID: 40219258
That worked, thx
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 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