Assassin87
Assassin87

Reputation: 295

Why is my nested CustomerViewModel null?

I managed to register an applicationuser, and now I would like to make an HTTP GET request which displays the current logged in user's account details in a View.

However, When I run the application and go to the "MyPages" View, I get an exception saying that the nested CustomerViewModel is null. I have absolutely no idea why but for some reason it is.

This is the actionresult in the HomeController.cs

public ActionResult MyPages()
{
    string userId = User.Identity.GetUserId();
    var currentUser = _userRepository.GetById(userId);
    var customer = _customerRepository.GetById(currentUser.Customer.Id);

    var userViewModel = new UserViewModel();
    userViewModel.Id = currentUser.Id;
    userViewModel.UserName = currentUser.UserName;

    //This is where the exception occurs. Apperantly CustomerViewModel is null                      
    userViewModel.CustomerViewModel.Id = customer.Id;
    userViewModel.CustomerViewModel.FirstName = customer.FirstName;
    userViewModel.CustomerViewModel.LastName = customer.LastName;
    userViewModel.CustomerViewModel.Email = customer.Email;           

    return View(userViewModel);
}

This is the UserViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Microsoft.AspNet.Identity.EntityFramework;

namespace GUI.Models
{
    public class UserViewModel
    {
        public virtual string Id { get; set; }
        public virtual string UserName { get; set; }
        public virtual string PasswordHash { get; set; }
        public virtual string SecurityStamp { get; set; }

        public virtual ICollection<IdentityUserRole> Roles { get; set; }
        public virtual ICollection<IdentityUserClaim> Claims { get; set; }
        public virtual ICollection<IdentityUserLogin> Logins { get; set;  }

        public CustomerViewModel CustomerViewModel { get; set; }
    }
}

And this is the CustomerViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using ClassLibrary.Entities;

namespace GUI.Models
{
    public class CustomerViewModel
    {
        public Guid Id { get; set; }

        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Display(Name = "Email Adress")]
        [Required(ErrorMessage = "Email address is required")]
        [EmailAddress(ErrorMessage = "Invalid Email Address")]
        public string Email { get; set; }

        public List<InvoiceViewModel> InvoiceViewModels { get; set; }
        public List<AddressViewModel> AdressViewModels { get; set; }

        public AddressViewModel AdressViewModel { get; set; }

        public virtual ICollection<Invoice> Invoice { get; set; }
        public virtual ICollection<Adress> Adress { get; set; }
    }
}

What seems to be the problem here?

Upvotes: 1

Views: 96

Answers (2)

Harikant
Harikant

Reputation: 277

Please replace your UserViewModel Model class with below code

public class UserViewModel

{

    public virtual string Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string PasswordHash { get; set; }
    public virtual string SecurityStamp { get; set; }
    public UserViewModel()
    {
        CustomerViewModel = new CustomerViewModel();
    }

    public CustomerViewModel CustomerViewModel { get; set; }
}

Upvotes: 0

Jason Evans
Jason Evans

Reputation: 29186

I might be missing something in your code, but it does not look like a new CustomerViewModel is instantiated for userViewModel.CustomerViewModel.

You could save some hassle and let UserViewModel create a new instance for you:

public class UserViewModel
{
    public UserViewModel()
    {
        CustomerViewModel = new CustomerViewModel();
    }

    public CustomerViewModel CustomerViewModel { get; set; }
}

Upvotes: 2

Related Questions