Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC DropDownListFor SelectList Default Selected Value Issues?

Posted on 2014-07-23
2
Medium Priority
?
5,130 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 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

773 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