无法为我的Html.DropDownList设置默认值

时间:2014-05-06 15:03:56

标签: html asp.net asp.net-mvc razor html-helper

我在asp.net mvc web应用程序中有以下内容: -

 <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>

目前DropdownList忽略了应该等于ViewBag.pagesize的默认值。即使我手动指定defualt值,它也将被忽略,如下所示!!!: -

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

有人可以建议为什么我无法为Html.DropdownList指定默认值吗? 感谢

编辑以下是我构建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; }

1 个答案:

答案 0 :(得分:1)

只需确保ViewBag中的属性名称与DropDownList的名称​​不同。   @Html.DropDownList("pagedsizeoptions", new SelectList(ViewBag.PagedSizeOptionsSelectList, "Value", "Text", ViewBag.pagesize ), new { @id= "pagedsizeoptions",@class="SmallDropDown3"})

有一些关于ViewBag中名称的约定以及打破选择值的下拉列表的名称:ASP.NET MVC Html.DropDownList SelectedValue

即只是这样: @Html.DropDownList("PagedSizeOptions")如果ViewBag中有一个名为PagedSizeOptions的选择列表

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

相关问题