Asp.net mvc 3 无法对部分视图中的空引用错误执行运行时绑定

Asp.net mvc 3 无法对部分视图中的空引用错误执行运行时绑定,asp.net-mvc-3,asp.net-membership,Asp.net Mvc 3,Asp.net Membership,我在局部视图中得到这个错误 Cannot perform runtime binding on a null reference error 这是我产生错误的部分观点 loginUserExists = Membership.ValidateUser(UserNameLogin, PasswordUserLogin); if (loginUserExists==false) { return; } FormsAuthentication.SetAuthCookie(UserNameLo

我在局部视图中得到这个错误

Cannot perform runtime binding on a null reference error
这是我产生错误的部分观点

loginUserExists = Membership.ValidateUser(UserNameLogin, PasswordUserLogin);
if (loginUserExists==false)
{
    return;
}
FormsAuthentication.SetAuthCookie(UserNameLogin, true);
@*这就是错误发生的地方*@
**@如果(!Page.User.Identity.IsAuthenticated)**
{
  • } 其他的 {
  • if(Model.FirstName!=null) {
  • @Model.FirstName
  • } }
    局部视图嵌套在布局页面中

    关于我为什么会这样以及如何克服它的见解

    关于

    请尝试:

    @*This is where the error happens*@
    **@if (!Page.User.Identity.IsAuthenticated)**
    {
        <li><a href="@Url.Action("LoginForm", "Home")">Login</a></li>
        <li><a href="@Url.Action("RegisterForm", "Home")">Register</a></li>
    }
    else
    {
    
        <li><a href="@Url.Action("LoginForm", "Home")">Logout</a></li>
        if (Model.FirstName != null)
        {
            <li><label id="labUserName">@Model.FirstName</label></li>
        }
    }
    
    @if (!Request.IsAuthenticated)
    {
        // ...
    }