ASP MVC 4:所选选项不在@Html.DropDownList 的顶部,具有 html 属性
ASP MVC4 : Selected option is not at top in @Html.DropDownList with html attributres
我的“~/viewName/Controllers/edit.cshtml”页面中有一个编辑页面。
从 Controller 的编辑操作中,DropDownList 数据来自此 Viewbag:
ViewBag.PROJECT_NO = new SelectList(db.TRAINING_PROJECT, "PROJECT_NO", "PROJECT_NAME",
training_new.PROJECT_NO);
当我进入编辑页面时,默认
@Html.DropDownList("PROJECT_NO", String.Empty)
在底部显示相应的选定值和其他值。当我添加一些 html 属性,如 class、id、required、disabled 然后从这个 answer 我在我的 "edit.cshtml"
中做了下面的代码
@Html.DropDownList("PROJECT_NO", null, String.Empty, new {@class = "form-control input-sm",
@id = "ex2" })
现在我的 DropDownList 不显示所选值。相反,它显示空值,意味着空白作为选定值和底部的其他值。但是在这里我想看到选中的那个。下图将更多地描述默认输出和第二个输出
试试这个:
IEnumerable<SelectListItem> selectList =
from c in db.TRAINING_PROJECT
select new SelectListItem
{
Selected = (c.ProjectId == 1), //put your condition here
Text = c.ProjectName , //MyAssumption, put yours here
Value = c.ProjectId //My assumption, put yours here
};
ViewBag.PROJECT_NO = selectList;
您使用了错误的重载,您需要使用 this overload 如:
@Html.DropDownList("PROJECT_NO",
ViewBag.PROJECT_NO as SelectList,
new { @class = "form-control input-sm", id = "ex2" })
旁注:
id前面不需要加前缀@
,我们放if for class
因为class
是保留字c#,所以为了将它用作变量,我们需要在它前面加上前缀 @
试试这个:
@Html.DropDownList("PROJECT_NO", null, new {@class = "form-control input-sm",@id = "ex2" })
写的时候
@Html.DropDownList("PROJECT_NO", null, String.Empty, new {@class = "form-control input-sm",@id = "ex2" })
它将 String.Empty 作为 DropDownList header。
我的“~/viewName/Controllers/edit.cshtml”页面中有一个编辑页面。 从 Controller 的编辑操作中,DropDownList 数据来自此 Viewbag:
ViewBag.PROJECT_NO = new SelectList(db.TRAINING_PROJECT, "PROJECT_NO", "PROJECT_NAME",
training_new.PROJECT_NO);
当我进入编辑页面时,默认
@Html.DropDownList("PROJECT_NO", String.Empty)
在底部显示相应的选定值和其他值。当我添加一些 html 属性,如 class、id、required、disabled 然后从这个 answer 我在我的 "edit.cshtml"
中做了下面的代码@Html.DropDownList("PROJECT_NO", null, String.Empty, new {@class = "form-control input-sm",
@id = "ex2" })
现在我的 DropDownList 不显示所选值。相反,它显示空值,意味着空白作为选定值和底部的其他值。但是在这里我想看到选中的那个。下图将更多地描述默认输出和第二个输出
试试这个:
IEnumerable<SelectListItem> selectList =
from c in db.TRAINING_PROJECT
select new SelectListItem
{
Selected = (c.ProjectId == 1), //put your condition here
Text = c.ProjectName , //MyAssumption, put yours here
Value = c.ProjectId //My assumption, put yours here
};
ViewBag.PROJECT_NO = selectList;
您使用了错误的重载,您需要使用 this overload 如:
@Html.DropDownList("PROJECT_NO",
ViewBag.PROJECT_NO as SelectList,
new { @class = "form-control input-sm", id = "ex2" })
旁注:
id前面不需要加前缀@
,我们放if for class
因为class
是保留字c#,所以为了将它用作变量,我们需要在它前面加上前缀 @
试试这个:
@Html.DropDownList("PROJECT_NO", null, new {@class = "form-control input-sm",@id = "ex2" })
写的时候
@Html.DropDownList("PROJECT_NO", null, String.Empty, new {@class = "form-control input-sm",@id = "ex2" })
它将 String.Empty 作为 DropDownList header。