为什么我没有将对象引用设置为对象错误的实例?

时间:2019-05-08 16:14:29

标签: c# asp.net-mvc razor

我在_Layout页面中使用了一个基本视图模型。我还有另一个正文页面,需要在使用另一个模型视图的布局页面内部进行渲染,因此在此行上出现错误“对象引用未设置为对象的实例。” 第77行:@if(Model.Name ==“管理员”)

BaseViewModel:

public class ViewModelBase
{
        //Current user data
        public string Id { get; set; }
        public string UserName { get; set; }
        public string Name { get; set; }

        //Report data
        public string ReportName { get; set; }
        public string Brand { get; set; }
        public string FileName { get; set; }
        public string FilePath { get; set; }

        public virtual List<Report> Reports { get; set; }
}

_Layout.cshtml调用

@model FulfillmentPortal.ViewModels.ViewModelBase

正文页面:

@model FulfillmentPortal.ViewModels.RegisterViewModel
@{
    ViewBag.Title = "Fulfillment Portal - Register";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>
}

RegisterViewModel:

public class RegisterViewModel : ViewModelBase
{
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
}

我不确定为什么会出现此错误。请帮忙弄清楚。谢谢!

0 个答案:

没有答案