0

I have a base view model that I am using in my _Layout page. I have another body page that I need to render inside the layout page that uses another model view and so I get the error "Object reference not set to an instance of an object."on this line. Line 77: @if (Model.Name == "Administrator")

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 call

@model FulfillmentPortal.ViewModels.ViewModelBase

Body Page:

@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; }
}

I am not sure why I am getting this error. Please help figure this out. Thank you!

SGekko
  • 295
  • 1
  • 11

0 Answers0