Html.DropDownList selected value not working using ViewBag
Asked Answered
F

5

5

Well, after a couple hours reading stuff over here, trying unsuccessfully all the solutions, also found this article that i thought it would save my life... nothing.

Long story short.

Here is my View (all combinations)

@Html.DropDownList("yearDropDown",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDownxxx",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown",(<SelectList>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown")

Here is my Controller

public ActionResult(int year)
{
var years = new int[] { 2007, 2008, 2009, 2010, 2011, 2012 }
                .Select(x => new SelectListItem { 
                    Text = x.ToString(), 
                    Value = x.ToString(), 
                    Selected=x==year }).Distinct().ToList();

            years.Insert(0, new SelectListItem { Value = null, Text = "ALL YEARS" });
            ViewBag.yearDropDown = new SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault());
return View();
}

Here is my rendered HTML. Selected nowhere to be found.

<select id="yearDropDown" name="yearDropDown"><option value="">ALL YEARS</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
</select>

Needless to mention, but i will, i checked in my Watch and SelectList actually has SelectedValue property populated with the selected year passed to the controller. But when i renders at the view, it goes to the first option.

Please, I need the solution for DropDownList, NOT for DropDownListFor. I am highlighting this because i saw other people here asking for the same help and bunch of people gave them instructions, and almost order them, to use DropDownListFor. There is a reason why i NEED to use DropDownList.

SOLUTION: Look at my own answer. However, here are the simple changes i made.

Controller:

ViewBag.yearDropDown = years;

View:

@Html.DropDownList("yearDropDown")
Fredericafrederich answered 28/3, 2013 at 15:57 Comment(0)
B
22

The problem may also be the name, see here.

In the Controller

ViewBag.PersonList= new SelectList(db.Person, "Id", "Name", p.PersonId); 

In the View

@Html.DropDownList("PersonList",(SelectList)ViewBag.PersonList )

This will not work, you have to change the name, so it's not the same.

@Html.DropDownList("PersonList123",(SelectList)ViewBag.PersonList )

So change yearDropDown and it Will Work for you.

Best regards Christian Lyck.

Ball answered 3/11, 2013 at 0:54 Comment(1)
on edit, how to know the selected one(which is selected) from the listedZeitgeist
F
2
ViewBag.yearDropDown = new SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault());

Last parameter here is SelectListItem, but it must be selected value (string in your example)

SelectList Constructor (IEnumerable, String, String, Object)

Floatage answered 28/3, 2013 at 16:28 Comment(1)
Interesting... i haven't tested it but i think you are right. i was returning the whole object. ThanksFredericafrederich
T
1

I know its an old question, but I will share another solution since sometimes you have done anything correctly, but you can't see the selected value after a request and response, since your razor has bug maybe. In this condition, that I've had, you can use select-option using viewbag:

In controller I had:

 ViewBag.MyData = GetAllData(strSelected);
... 

and as a private function in controller I had:

  private List<SelectListItem> GetAllData(List<string> selectedData)
    {

        return MyGetAll().Select(x =>
            new SelectListItem
            {
                Text = x.Point,
                Value = x.Amount.ToString(),
                Selected = selectedPrizes.Contains(x.Amount.ToString())
            })
            .ToList();
    }  

And in .cshtml I had:

<select id="Amount" name="Amount" multiple="multiple">
  @foreach (var listItem in (List<SelectListItem>)ViewBag.MyData)
   {
     <option value="@listItem.Value" @(listItem.Selected ? "selected" : "")>
       @listItem .Text
     </option>
   }
</select>

I hope it will be useful for similar problems :)

Trying answered 18/7, 2016 at 5:26 Comment(0)
P
0

Change the name of the ViewBag property to NOT match the name of the DropDownList. Use ViewBag.yearDropDownDD instead of ViewBag.yearDropDown.

ALSO, you are creating the SelectList wrongly. You need to pass the selected value not the array item:

ViewBag.yearDropDown = new
SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault().Value);
Puree answered 28/3, 2013 at 16:5 Comment(2)
If you look at my second example in the View, that's exactly what i did. Didn't help either. Found the solution thou. Look at my own answer. Thanks for the time and attention thou.Fredericafrederich
@Tony, And what was the solution?Puree
F
-1

The first place i should've gone to learn.... and i didn't.

Here is my solution

Thank you thou for your answers. Really appreciate it. I feel like shooting my own foot now. Two hours reading blogs and it took me 2 minutes to replicate this example and worked perfectly...

Fredericafrederich answered 28/3, 2013 at 16:11 Comment(1)
And what was the solution?Puree

© 2022 - 2024 — McMap. All rights reserved.