Anders Jansson
Anders Jansson

Reputation: 29

@Html.DropDownList fails on Selected = true

It seems that the @Html.DropDownList fails to render with Selected value set. I have an example:

@foreach (SelectListItem item in ViewBag.Agare as List<SelectListItem>)
{
    <div>
        Item: Name: @(item.Text) Value: @(item.Value) Selected: @(item.Selected)
    </div>
}
<div class="editor-field">
    @Html.DropDownList("Agare", ViewBag.Agare as List<SelectListItem>, new {@class = "chzn-select" })
</div>

And the result is :

Name: Demo Yacht Club Value: 1 Selected: False

Name: Anders Jansson Value: 100 Selected: False

Name: Per Kyrk Value: 101 Selected: False

Name: Oskar Persson Value: 102 Selected: True

Name: Pluten Snutten Value: 204 Selected: False

And the first options is always default value!

Rendered code is:

  <select class="chzn-select" id="Agare" name="Agare" required="required">
    <option value="1">Demo Yacht Club</option>
    <option value="100">Anders Jansson</option>
    <option value="101">Per Kyrk</option>
    <option value="102">Oskar Persson</option>
    <option value="204">Pluten Snutten</option>
 </select>

Its worth mentioning that I also use Choosen to style the DropDown, but it does not matter if I stop using it. Otherwise it is very straight forward everything!

And my versions are: Visual Studio Express 2013 for Web. MVC 5.0.0 Asp.Net Razor 3.0.0 Asp.Net Web API 5.0.0 Asp:net Web Pages 3.0.0 EF 6.0.1 chosen 0.12.0

Any Ideas?

Upvotes: 0

Views: 1356

Answers (3)

Anders Jansson
Anders Jansson

Reputation: 29

Changed name of ViewBag property, and now it works.

@Html.DropDownList("Agare", ViewBag.ListOfAgare as List<SelectListItem>, new { required = "required", @class = "chzn-select" })

Why is that?

Upvotes: 0

Lin
Lin

Reputation: 15188

Don't use ViewBag. Having a better solution is more important than just making the problem work. If you search the internet for "ViewBag SelectListItem not working", you can find a lot, that is why I am suggesting you to use VIewModel.

below is an example.

ViewModel

public class SportsViewModel
{
    public string SelectedSports { get; set; }
    public IEnumerable<SelectListItem> Sportses { get; set; } 
}

Action

public ActionResult Sport()
{
    var model = new SportsViewModel();
    //_sportses is a list for sports
    // get selectedSprortsId value for dropdown selected item. 
    model.Sportses = _sportses.Select(m => new SelectListItem {Value = m.Id.ToString(), Text = m.Name, Selected = m.Id == selectedSprortsId});

       return View(model);
}

View

@model SportsViewModel

@foreach (var item in Model.Sportses)
{
    <div>
        Item: Name: @(item.Text) Value: @(item.Value) Selected: @(item.Selected)
    </div>
}
<div class="editor-field">
    @Html.DropDownList("SelectedSports", Model.Sportses, new { @class = "chzn-select" })
</div>

Update:

If you cann't change to ViewModel, you can use SelectList,and set all list to your ViewBag like below:

   var SelectListItemList = (from s in _sportses
                      select new SelectListItem
                      {
                          Text = s.Name,
                          Value = s.Id.ToString(),
                      }).ToList();

    ViewBag.Agare = new SelectList(SelectListItemList , "Value", "Text", selectedSprortsId);

Then in your view, you still can your existing code just delete "as List<SelectListItem>".

@foreach (SelectListItem item in ViewBag.Agares)
{
    <div>
        Item: Name: @(item.Text) Value: @(item.Value) Selected: @(item.Selected)
    </div>
}
<div class="editor-field">
 @Html.DropDownList("Agare", ViewBag.Agare as List<SelectListItem>, new { @class = "chzn-select" })

Upvotes: 0

5te
5te

Reputation: 81

Please try changing the name of your DropDownList 'Agare' to be different from your ViewBag property. Strange, I know!

@Html.DropDownList("SomethingElse", ViewBag.Agare as List<SelectListItem>, new {@class = "chzn-select" })

Upvotes: 2

Related Questions