在将DropDownList设置为它之前检查ViewData是否为null

时间:2012-04-09 16:39:20

标签: asp.net-mvc

我有这个:

<% if (ViewData["roots"] != null) {%>
       <%Html.DropDownList("roots"); %>
<%}%>

但它不起作用。如何检查ViewData是否存在?

2 个答案:

答案 0 :(得分:4)

尝试

ViewData.ContainsKey("roots")

答案 1 :(得分:3)

您没有输出任何内容并且错误地使用DropDownList帮助程序。试试这样:

<% if (ViewData["roots"] != null) { %>
       <%= Html.DropDownList("roots") %>
<% } %>
相关问题