C# 使用选项值将项目添加到@Html.DropDownList,并将默认设置为选中

C# 使用选项值将项目添加到@Html.DropDownList,并将默认设置为选中,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我正在视图中呈现选择列表,如下所示: @Html.DropDownList("SelectCategory", (SelectList)ViewBag.Categories, "All") 我这样填充它: ViewBag.Categories = new SelectList(db.Categories, "Id", "Name"); 这意味着: <select id="SelectCategory" name="SelectCategory"> <option value

我正在视图中呈现选择列表,如下所示:

@Html.DropDownList("SelectCategory", (SelectList)ViewBag.Categories, "All")
我这样填充它:

ViewBag.Categories = new SelectList(db.Categories, "Id", "Name");
这意味着:

<select id="SelectCategory" name="SelectCategory">
<option value="">All</option>
<option value="1">Fruit</option>
<option value="44">T-Shirts</option>
</select>

全部的
果
T恤衫
问题:

1)
All
的选项值为空,如何将值放在那里,比如说
0


2) 如何在
@Html.DropDownList
中设置默认的选定值?

您可以“手动”创建选定值

然后可以在Html.DropdownList中使用该列表 下面是完整的例子

int catId = // Gets the catId to select somehow
IEnumerable<SelectListItem> options = optionsList
    .Select(x => new SelectListItem { 
         Text = x.Text, 
         Value = x.Value, 
         Selected = x.Value == catId
     }); // catId 

DropDownList()
方法的第三个参数添加了一个带有
null
值的“label”选项。通常,选项文本类似于“请选择”,其目的是强制用户进行有效选择。如果选择标签选项,则提交一个
null
值,并且
ModelState
无效(假设需要绑定到的属性)

如果您想要一个带有
All
的附加选项,那么您需要在
选择列表中生成该选项,例如,您可以访问视图

List<SelectListItem> categories = db.Categories.Select(x => new SelectListItem()
{
    Value = x.Id.ToString(), // assumes Id is not already typeof string
    Text = x.Name
}).ToList();
categories.Insert(0, new SelectListItem(){ Value = "0", Text = "All" }) // Or .Add() to add as the last option
ViewBag.Categories = categories;
List categories=db.categories.Select(x=>new-SelectListItem()
{
Value=x.Id.ToString(),//假设Id不是字符串的类型
Text=x.名称
}).ToList();
categories.Insert(0,new SelectListItem(){Value=“0”,Text=“All”})//或.Add()作为最后一个选项添加
ViewBag.Categories=类别;
在视图中(注意,删除第三个参数是您不需要标签选项)

@Html.DropDownList(“SelectCategory”,(IEnumerable)ViewBag.Categories,“请选择”)

为了最初“选择”一个选项,您需要在将模型传递给视图之前设置绑定到的属性值,因此如果属性
SelectCategory
的值为
“0”
,则在首次显示视图时将选择“All”选项。如果其
“44”
,则将选择“T恤”选项。如果
SelectCategory
的值与其中一个选项值不匹配,或者
null
,则将选择第一个选项(因为必须选择此选项)

您需要显示如何从控制器填充SelectList。@Yanga刚刚做了,很抱歉。是的,但如果可能的话,我想使用@Html.DropDownList。另一个选项是使用LinqThanks在视图中构建列表选项,如果@Html.DropDownList无法处理此问题,我将接受您的回答。一个好的做法是尽可能避免控制器中的此类代码,以便如果出于任何原因需要更改UI(例如显示单选按钮而不是DropDownList),您只需编辑视图。我写了如何填充select,你能在上一次编辑中给出一个完整的例子吗?而
myCondition
是一个int catId参数,通过GET提供。我不确定我是否可以使用DropDownListFor,因为我已经在该视图中使用了一个模型。这个看起来很棒,明天会尝试,只是我的问题中缺少一点:如何设置默认值?我在我的POST操作中得到了一个id,我会用它来选择一个选项。请参阅答案的最后一段。如果在将模型传递给视图之前,在GET方法中设置了property
SelectCategory=0
,则将选择与该值匹配的选项(
“All”
),这就是模型绑定的工作方式,它将绑定到属性的值。我不确定你所说的我在我的POST操作中获得id是什么意思,我会用它来选择一个选项(POST方法与此有什么关系?)我很抱歉时间太晚了,我是说获得,我只是说我在那里获得了一个我可以使用的值。好的,我会先测试一下。
int catId = // Gets the catId to select somehow
IEnumerable<SelectListItem> options = optionsList
    .Select(x => new SelectListItem { 
         Text = x.Text, 
         Value = x.Value, 
         Selected = x.Value == catId
     }); // catId 
 @Html.DropdownList("id-of-the-dropdown", options);
List<SelectListItem> categories = db.Categories.Select(x => new SelectListItem()
{
    Value = x.Id.ToString(), // assumes Id is not already typeof string
    Text = x.Name
}).ToList();
categories.Insert(0, new SelectListItem(){ Value = "0", Text = "All" }) // Or .Add() to add as the last option
ViewBag.Categories = categories;
@Html.DropDownList("SelectCategory", (IEnumerable<SelectListItem>)ViewBag.Categories, "Please select")