如何在 MVC 的 DropDownList 中将默认值“0”设置为文本 "Select"?
How to set default value "0" to text "Select" in DropDownList in MVC?
下面是国家的下拉列表。我想要选定的文本 "Select",它正在工作。
@Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
new { @class = "form-control" })
现在我想为默认选中的文本 "Select" 设置值“0”。当前 "Select" 的值为空,如下所示。
我该怎么做?值 "Select" 不在数据源中。我必须在 JQuery.
中访问这个选定的值
我试过这两个,但其中 none 有效。
@Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
"Select", new { @class = "form-control", @selected = "0" })
和
@Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
"Select", new { @class = "form-control", @selected = "0" })
下面是 CountryName 值的控制器代码
ViewBag.CountryName = dbLMS.CountryMasters.Select(c => new { c.CountryId, c.CountryName }).OrderBy(c => c.CountryName).ToList();
new SelectList(ViewBag.CountryName, "CountryId", "CountryName", //Default value)
您可以进行如下操作:
选项-1:
@{
var countrySelectList = new SelectList(ViewBag.CountryName, "CountryId", "CountryName");
List<SelectListItem> countrySelectListItems = countrySelectList.ToList();
countrySelectListItems.Insert(0, (new SelectListItem { Text = "Please select", Value = "0", Selected = true }));
}
@Html.DropDownList("ddlCountryName", countrySelectListItems , new { @class = "form-control" })
选项-2:
在控制器方法中:
List<SelectListItem> selectListItems = dbLMS.CountryMasters.Select(a => new SelectListItem()
{
Text = a.CountryName,
Value = a.CountryId
}).ToList();
selectListItems.Insert(0, new SelectListItem(){Text = "Selet Country", Value = "0", Selected = true});
ViewBag.CountrySelectList = selectListItems;
然后在视图中:
@Html.DropDownList("ddlCountryName", (List<SelectListItem>)ViewBag.CountrySelectList, new { @class = "form-control" })
您真的不需要 Select 的值。对于模型,保留 Required validation 属性。
这将确保选择该值。这样您就不必在后端签入。
下面是国家的下拉列表。我想要选定的文本 "Select",它正在工作。
@Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
new { @class = "form-control" })
现在我想为默认选中的文本 "Select" 设置值“0”。当前 "Select" 的值为空,如下所示。
我该怎么做?值 "Select" 不在数据源中。我必须在 JQuery.
中访问这个选定的值我试过这两个,但其中 none 有效。
@Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
"Select", new { @class = "form-control", @selected = "0" })
和
@Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
"Select", new { @class = "form-control", @selected = "0" })
下面是 CountryName 值的控制器代码
ViewBag.CountryName = dbLMS.CountryMasters.Select(c => new { c.CountryId, c.CountryName }).OrderBy(c => c.CountryName).ToList();
new SelectList(ViewBag.CountryName, "CountryId", "CountryName", //Default value)
您可以进行如下操作:
选项-1:
@{
var countrySelectList = new SelectList(ViewBag.CountryName, "CountryId", "CountryName");
List<SelectListItem> countrySelectListItems = countrySelectList.ToList();
countrySelectListItems.Insert(0, (new SelectListItem { Text = "Please select", Value = "0", Selected = true }));
}
@Html.DropDownList("ddlCountryName", countrySelectListItems , new { @class = "form-control" })
选项-2:
在控制器方法中:
List<SelectListItem> selectListItems = dbLMS.CountryMasters.Select(a => new SelectListItem()
{
Text = a.CountryName,
Value = a.CountryId
}).ToList();
selectListItems.Insert(0, new SelectListItem(){Text = "Selet Country", Value = "0", Selected = true});
ViewBag.CountrySelectList = selectListItems;
然后在视图中:
@Html.DropDownList("ddlCountryName", (List<SelectListItem>)ViewBag.CountrySelectList, new { @class = "form-control" })
您真的不需要 Select 的值。对于模型,保留 Required validation 属性。
这将确保选择该值。这样您就不必在后端签入。