Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Html5中显示下拉列表的选定值_Javascript_Jquery_Asp.net Mvc_Html - Fatal编程技术网

Javascript 在Html5中显示下拉列表的选定值

Javascript 在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
“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)