Solved

MVC DropDownListFor SelectList Default Selected Value Issues?

Posted on 2014-07-23
2
4,670 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
2 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 400 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

20 Experts available now in Live!

Get 1:1 Help Now