Solved

MVC DropDownListFor SelectList Default Selected Value Issues?

Posted on 2014-07-23
2
4,914 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 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 DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

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!
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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