Solved

MVC DropDownListFor SelectList Default Selected Value Issues?

Posted on 2014-07-23
2
4,625 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

18 Experts available now in Live!

Get 1:1 Help Now