为什么 =ViewData[""] 显示一个字符串,但对同一字符串进行评估却失败?

发布于 2024-10-06 04:44:52 字数 1331 浏览 3 评论 0 原文

//CHECK IF WE SHOULD SHOW THE PASSWORD HINT OR NOT
Setting passwordhints;

using (var db = new dbDataContext())
{
    passwordhints = (from c in db.Settings
            where c.Name == "ShowPasswordHints" && c.ID == _ID
            select c).FirstOrDefault();
}

if (passwordhints != null)
    //NOTE: .Value IS A STRING
    ViewData["ShowPasswordHints"] = passwordhints.Value;
else
    ViewData["ShowPasswordHints"] = "False";

//END PASSWORD HINTS CHECK

在控制器中,当我到达页面本身时,我将

<%=ViewData["ShowPasswordHints"]%> 输出到标题标签中,我可以看到它在那里显示“True” (没有引号,我还通过用括号将其括起来来检查空格,并且没有空格,它实际上就是 True)

但是,当我确实

<%if(ViewData["ShowPasswordHints"] == "True") {%> SHOW THIS <%}%>

SHOW THIS 从未出现时,到底是什么?

更新:但是,如果 ViewData 像这样设置...它可以工作...嗯?

if (accountRepository.isLDAPEnabled(_ID))
                ViewData["LDAP"] = "True";
            else
                ViewData["LDAP"] = "False";

查看...

<%if(ViewData["LDAP"] == "True"){ %>
           SHOW THIS
         <%} %>

谢谢大家,这是非常有效的新方法

ViewData["something"] = true;

<%if(true.Equals(ViewData["something"])){%> SHOW THIS <%}%>
//CHECK IF WE SHOULD SHOW THE PASSWORD HINT OR NOT
Setting passwordhints;

using (var db = new dbDataContext())
{
    passwordhints = (from c in db.Settings
            where c.Name == "ShowPasswordHints" && c.ID == _ID
            select c).FirstOrDefault();
}

if (passwordhints != null)
    //NOTE: .Value IS A STRING
    ViewData["ShowPasswordHints"] = passwordhints.Value;
else
    ViewData["ShowPasswordHints"] = "False";

//END PASSWORD HINTS CHECK

is in the controller, when I get to the page itself I output

<%=ViewData["ShowPasswordHints"]%> into the title tag and I can see it up there it says "True" (without quotes, I also checked for spaces by surrounding it with parenthesis and there are no spaces it is literally just True)

However when I do

<%if(ViewData["ShowPasswordHints"] == "True") {%> SHOW THIS <%}%>

SHOW THIS never appears, what the hell?

UPDATE: However, if ViewData is set like this... IT WORKS... HUH??

if (accountRepository.isLDAPEnabled(_ID))
                ViewData["LDAP"] = "True";
            else
                ViewData["LDAP"] = "False";

view...

<%if(ViewData["LDAP"] == "True"){ %>
           SHOW THIS
         <%} %>

THANKS EVERYONE, HERE IS NEW METHOD THAT IS WORKING GREAT

ViewData["something"] = true;

<%if(true.Equals(ViewData["something"])){%> SHOW THIS <%}%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2024-10-13 04:44:52

由于 ViewDataIDictionary,因此 ViewData["ShowPasswordHints"] 的类型为 object。默认情况下,对象通过引用进行比较。您想要进行价值比较。因此,尝试将其转换为字符串,这将导致值比较:

<%if((string)ViewData["ShowPasswordHints"] == "True") {%> SHOW THIS <%}%>

更多信息请参见msdn

更新:您放入 ViewData["ShowPasswordHints"] 中的值始终是一个字符串。然而,由于 C# 是静态类型的,编译器不知道当你把它取出来时,它是一个字符串——它只知道它将是一个对象,因为 ViewData 是一个 < code>IDictionary (返回普通对象的字典)。但既然您知道得更清楚,您可以将其转换为您知道应该是的字符串。 (顺便说一句,我认为 ViewData 是 MVC 框架的最弱点之一,出于这个原因和其他原因)

为什么这在 </code> 中起作用是因为 <code><%= % ></code> 标签对其中的任何内容调用 <code>ToString()</code> 。由于 <code>ViewData["ShowPasswordHints"]</code> 是一个字符串,因此它的结果就像您所期望的那样 - 字符串的 <code>ToString()</code> 是字符串本身。

最后:为什么不使用布尔值?

Since ViewData is an IDictionary<string, object>, ViewData["ShowPasswordHints"] is of type object. By default, objects are compared by reference. You want a value comparison. Thus, try casting it to a string, which will cause a value comparison:

<%if((string)ViewData["ShowPasswordHints"] == "True") {%> SHOW THIS <%}%>

More at msdn.

UPDATE: The value you put into ViewData["ShowPasswordHints"] is always a string. However, since C# is statically typed, the compiler doesn't know that when you take it back out, it is a string -- it only knows that it will be an object, since ViewData is a IDictionary<string, object> (a dictionary returning plain objects). But since you know better, you can cast it to the string that you know it should be. (BTW, I think ViewData is one of the weakest points of the MVC framework, for this reason and others)

Why this works in the <title> is because the <%= %> tags call ToString() on whatever in them. Since ViewData["ShowPasswordHints"] is a string, it comes out like you'd expect -- ToString() of a string is the string itself.

Finally: why aren't you using a boolean?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文