Javascript 在Html5中显示下拉列表的选定值
我有两个ViewBagJavascript 在Html5中显示下拉列表的选定值,javascript,jquery,asp.net-mvc,html,Javascript,Jquery,Asp.net Mvc,Html,我有两个ViewBag“ViewBag.WorksheetName”和“ViewBag.selectedName” “ViewBag.workSheetNames”包含下拉列表的选项值,“ViewBag.selectedName”包含必须在下拉列表中显示为选中的名称。我使用给定的代码执行此操作:-- @foreach(ViewBag.workSheetNames中的变量项) { if(item==ViewBag.selectedName) { @项目 } 其他的 { @项目 } }
“ViewBag.WorksheetName”
和“ViewBag.selectedName”
“ViewBag.workSheetNames”
包含下拉列表的选项值,“ViewBag.selectedName”
包含必须在下拉列表中显示为选中的名称。我使用给定的代码执行此操作:--
@foreach(ViewBag.workSheetNames中的变量项)
{
if(item==ViewBag.selectedName)
{
@项目
}
其他的
{
@项目
}
}
请给出任何有效的方法。您可以使用Mvc本身的HtmlHelpers来实现这一点。我想出了一个小例子:
@{
ViewBag.SelectedItem= "2";
ViewBag.List = new SelectList(
new List<object>()
{
new {Id = 1, Label = "option 1"},
new {Id = 2, Label = "option 2"}
}, "Id", "Label", ViewBag.SelectedItem);
}
@Html.DropDownList("DropdownListId", (SelectList)ViewBag.List)
@{
ViewBag.SelectedItem=“2”;
ViewBag.List=新建SelectList(
新名单()
{
新的{Id=1,Label=“option 1”},
新建{Id=2,Label=“option 2”}
},“Id”,“标签”,查看包。选择编辑项);
}
@DropDownList(“DropdownListId”,(SelectList)ViewBag.List)
@{
ViewBag.SelectedItem= "2";
ViewBag.List = new SelectList(
new List<object>()
{
new {Id = 1, Label = "option 1"},
new {Id = 2, Label = "option 2"}
}, "Id", "Label", ViewBag.SelectedItem);
}
@Html.DropDownList("DropdownListId", (SelectList)ViewBag.List)