1

I have the following inside my asp.net mvc web application :-

 <div class="SmallDropDown2 b" >

    @ViewBag.pagesize // to test the value Show  
    @Html.DropDownList("pagedsizeoptions", new SelectList(ViewBag.PagedSizeOptions, "Value", "Text", ViewBag.pagesize ), new { @id= "pagedsizeoptions",@class="SmallDropDown3"}) per page. 
    <img src="~/Content/sortloading.gif" class="loadingimage" id="progressSort3" /></div>

currently the DropdownList is ignoring the default value which should equal to ViewBag.pagesize. and even if i manually specify the defualt value , it will also be ignored, as follow !!!:-

@Html.DropDownList("pagedsizeoptions", new SelectList(ViewBag.PagedSizeOptions, "Value", "Text", 100 ), new { @id= "pagedsizeoptions",@class="SmallDropDown3"})

Can anyone advice why i am unable to specify a default value for the Html.DropdownList ? Thanks

EDIT here is how i am building the SelectList :-

 public PageOptions()
        {
            int size = Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);
            FilterOptions = new SelectList(
            new List<SelectListItem> {
            new SelectListItem { Text=size.ToString(), Value = size.ToString()},
            new SelectListItem { Text="50", Value = "50"}, 
            new SelectListItem { Text="100", Value = "100"},
            new SelectListItem { Text="200", Value = "200"},
            new SelectListItem { Text="500", Value = "500"}
        }, "Value", "Text");

        }

        public SelectList FilterOptions { get; set; }
John John
  • 1
  • 72
  • 238
  • 501
  • @Jorge and idea what is causing this ? – John John May 06 '14 at 15:30
  • 1
    the values of the dropdown are passed as a string or int? – Jorge May 06 '14 at 15:34
  • i have updated my original question , with the code showing how i am building the Selectlist .. thanks – John John May 06 '14 at 15:39
  • Why do not test from the controller when you build the SelectList, adding the Default true, in the "1000" – Jorge May 06 '14 at 15:41
  • Didn't we just have this question? You now seem to have a mismatch of selection methods. Where is your code for setting `ViewBag.PagedSizeOptions` – iCollect.it Ltd May 06 '14 at 15:52
  • i think the problem is that the ViewBag.PagedSizeOptions name and the Html.DropDownList name is the same , i rename the DropdownList to be "FilterSize" instead of "pagedsizeoptions " and it worked well !!!! the strangest behavior i have ever had ... – John John May 06 '14 at 15:54

1 Answers1

1

Just make sure that the name of the property in the ViewBag is different than the DropDownList's name. @Html.DropDownList("pagedsizeoptions", new SelectList(ViewBag.PagedSizeOptionsSelectList, "Value", "Text", ViewBag.pagesize ), new { @id= "pagedsizeoptions",@class="SmallDropDown3"})

There's some conventions regarding the name of what's in the ViewBag and the name of the dropdownlist that break the seleced value: ASP.NET MVC Html.DropDownList SelectedValue

Namely that just this works: @Html.DropDownList("PagedSizeOptions") if you have a select list in the ViewBag named PagedSizeOptions

http://blinkingcaret.wordpress.com/2012/08/11/using-html-dropdownlistfor/

Community
  • 1
  • 1
Rui
  • 4,847
  • 3
  • 29
  • 35
  • In this case you should use the 4 parameter `SelectList` constructor that takes a default value (and not suggest applying `Selected` to an individual item). More flexible and simpler to write. – iCollect.it Ltd May 06 '14 at 15:58
  • @TrueBlueAussie You're right. Also, when the name of the dropdown is the same as the selectlist in the viewbag, the selected item doesn't get selected. I don't actually know why, I believe it has to do with the fact that `@Html.DropDownList("PagedSizeOptions", string.Empty)` would work if you have a select list in the viewbag with that name (PageSizeOptions) – Rui May 06 '14 at 16:08
  • This is right: the ViewBag name MUST be different than the model name of the dropdown, otherwise default value is ignored. – Alfred Wallace Dec 04 '18 at 22:02