【问题标题】:Using Html.DropDownList over a SelectList在 SelectList 上使用 Html.DropDownList
【发布时间】:2014-06-23 01:03:39
【问题描述】:

我的模型类中有以下代码:-

public class PageOptions
    {
        public PageOptions()
    {
      int  size = Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);
            NameSelectionOptions = 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 NameSelectionOptions { get; set; }
    }
}

但是我如何在 Html.DropDownList 中显示 SelectList ?并将默认值设置为大小? 谢谢

【问题讨论】:

  • 3年后,您对答案满意了吗? :)
  • 即将进入第五个年头......你还开心吗? :)

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


【解决方案1】:

您只需将所需的选择作为第四个参数传递给SelectList 构造函数:

NameSelectionOptions = 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", size);   // <<< Add size here

这比选择列表中的特定项目灵活得多。

有几个选项可以将列表绑定到视图。您可以在 ViewModel 中使用属性,但是标准做法(根据 Microsoft 的脚手架模板)是将下拉列表传递给 ViewBag 条目中的视图与 Model 属性同名。这有一个额外的好处,即自动将更简单的@Html.DropDownList("Size") 版本绑定到名为Size 的模型属性和ViewBag.Size 中的列表。

例如

在控制器中:

ViewBag.Size = 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", size);   // <<< Add size here
viewModel.Size = size;
return View(viewModel);

其中 viewModel 包含您要编辑的任何属性(包括Size)。

在视图中:

   @Html.DropDownList("Size")

或者如果您更喜欢强类型版本。

   @Html.DropDownListFor(m=>m.Size, (SelectList)ViewBag.Size)

无论如何,一致的命名将有助于避免问题。

默认值可以放在 ViewBag 中,但选择应该绑定到您的 ViewModel,以便您可以使用相同的 ViewModel 接收回发的值。

   @Html.DropDownListFor(m=>m.Size, (SelectList)ViewBag.Size, ViewBag.DefaultSize)

更新:

如果您不希望将当前值绑定到任何内容(根据注释),您只需将 ViewBag.Size 设置为控制器 SelectList 并拥有这是视图。您不需要模型中的值。

   @Html.DropDownList("Size")

默认选择将是上面new SelectList()中的选择(第4个参数,大小)。

【讨论】:

  • 好的,但我不想以任何方式绑定大小,因为此下拉列表用于过滤目的,用户将在其中选择记录页页数。所以我需要使用 Html.DropDownList 而不是 Html.DropDownListFor。但我不能将三个参数传递给 Html.SropdownList !!
  • 而且我目前的观点是使用不包含 anHtml.DropdownList 的 IPagedList
  • 您不需要提供绑定值。只需使用 @Html.DropDownList("Size") 就足以将列表以默认选定大小放在页面上。
【解决方案2】:

只需在其中添加 Selected 属性:

new SelectListItem { Text=size.ToString(), 
                     Value = size.ToString(),
                     Selected = true}

型号:

public class PageOptions
    {
        public PageOptions()
    {
      int  size = Int32.Parse("20");
            NameSelectionOptions = 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 NameSelectionOptions { get; set; }
    public string SelectedValue { get; set; }
    }

行动:

public ActionResult Index()
        {

            PageOptions model = new PageOptions();

            return View(model);
        }

强类型视图:

@model TestAjaxUpload.Models.PageOptions
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>


@Html.DropDownListFor(x=>x.SelectedValue,Model.NameSelectionOptions)

如果您想在不更改当前视图模型的情况下这样做,请在视图内创建实例并像这样传递:

@model SomeModelClass
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@{

    TestAjaxUpload.Models.PageOptions objModel = new TestAjaxUpload.Models.PageOptions();

}


@Html.DropDownListFor(x=>x.SelectedValue,objModel.NameSelectionOptions)

但你应该在 Model 中添加 PageOptions SelectList 作为 property 并使用它,我不建议直接在查看

更新(使用 ViewBag):

使用 ViewBag,您可以这样做:

 public ActionResult Index()
        {

            PageOptions model = new PageOptions();

            ViewBag.List = model.NameSelectionOptions;
            ViewBag.Selected = "20";

            return View(model);
        }

在视图中:

@Html.DropDownListFor(x=>x.SelectedValue,ViewBag.List as SelectList,ViewBag.Selected as string)

【讨论】:

  • 但是我如何在不使用 viewBag 的情况下在我的 DropDownlist 中引用 selectList ?
  • 你有绑定视图的模型吗?
  • 我确实想要任何模型的 PageOptions 部分,因为它只包含静态值,所以我需要直接引用 PageOption SelectList insdie my view?
  • 查看更新后的答案,希望对您有所帮助,有什么问题可以问我
  • 我想使用 viewBag 执行此操作并将默认值设置为等于其他 viewbag 的值,例如 @Html.DropDownList(ViewBag.PageOptions,"Text","Value",ViewBag.current )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多