2017-02-28 57 views
-1

我想在mvc5剃刀获取会话值,如下mvc5剃刀检查会话价值

@{var cnt=Session["count"].ToString();} 
@if (cnt == "0" || string.IsNullOrWhiteSpace(cnt)) 

这将导致一个错误,如果有会话没有价值。如何来检查

+1

的可能的复制[什么是一个NullReferenceException,如何解决呢?(http://stackoverflow.com/questions/4660142/ what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CodeCaster

+0

我真的很希望避免检查你的视图中的会话变量 – ediblecode

+0

当你必须显示替代html或视图时,你如何做到这一点在购物车中为空? – Diin

回答

1

简单地检查其空

@if (Session["count"] != null) { 
    //Do what you want 
}