在 SelectList 上使用 Html.DropDownList [英] Using Html.DropDownList over a SelectList
问题描述
我的模型类中有以下代码:-
I have the following code inside my model class :-
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 ?并将默认值设置为大小?谢谢
but how i can display the SelectList inside a Html.DropDownList ? and setting the default valueto be size? Thanks
推荐答案
您只需将所需的选择作为第四个参数传递给 SelectList
构造函数:
You simply pass the desired selection as a 4th parameter to the SelectList
constructor:
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
这比选择列表中的特定项目灵活得多.
This is far more flexible than selecting a specific item in the list.
将列表绑定到视图有几个选项.您可以在 ViewModel 中使用属性,但是标准做法(根据 Microsoft 的脚手架模板)是将下拉列表传递给 ViewBag 条目中的视图,与模型属性同名.这有一个额外的好处,即自动将更简单的 @Html.DropDownList("Size")
版本绑定到名为 Size
的模型属性和 ViewBag 中的列表.尺寸
.
There are several options for binding a list to the view. You can use a property in the ViewModel, however standard practice (as per Microsoft's scaffolding templates) is to pass dropdown lists to a view in a ViewBag entry of the same name as the Model property. This has the added bonus of automatically binding the simpler @Html.DropDownList("Size")
version to both a Model property called Size
and the list in 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
).
Where viewModel contains any properties you want edited (including Size
).
@Html.DropDownList("Size")
或者如果您更喜欢强类型版本.
or if you prefer the strongly typed version.
@Html.DropDownListFor(m=>m.Size, (SelectList)ViewBag.Size)
在任何情况下,一致的命名都有助于避免出现问题.
In any case the consistent naming will help avoid problems.
默认值可以放在 ViewBag 中,但选择应该绑定到您的 ViewModel,以便您可以使用相同的 ViewModel 接收回发的值.
Default values can go in the ViewBag, but the selection should be bound to your ViewModel so you can use the same ViewModel to receive the posted back values.
@Html.DropDownListFor(m=>m.Size, (SelectList)ViewBag.Size, ViewBag.DefaultSize)
更新:
如果您不想将当前值绑定到任何内容(根据评论),您只需将 ViewBag.Size
设置为您的 SelectList
控制器,这是视图.您不需要模型中的值.
Update:
If you do not wish to bind the current value to anything (as per comment), you simply need to have the ViewBag.Size
set to you SelectList
by the controller and have this is the View. You do not need a value in the Model.
@Html.DropDownList("Size")
默认选择将是上面new SelectList()
中的选择(第4个参数,大小).
The default selection will be the selection (4th parameter, size) in new SelectList()
above.
这篇关于在 SelectList 上使用 Html.DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!