我设法注册了一个applicationuser,现在我想发一个HTTP GET请求,在View中显示当前登录用户的帐户详细信息。
但是,当我运行应用程序并转到“MyPages”视图时,我得到一个异常,说嵌套的CustomerViewModel为null。我完全不知道为什么,但出于某种原因它是。
这是HomeController.cs中的actionresult
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);
}
这是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; }
}
}
这是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; }
}
}
这里似乎有什么问题?
答案 0 :(得分:2)
我可能会在您的代码中遗漏某些内容,但它似乎不会为CustomerViewModel
实例化新的userViewModel.CustomerViewModel
。
您可以节省一些麻烦,让UserViewModel
为您创建一个新实例:
public class UserViewModel
{
public UserViewModel()
{
CustomerViewModel = new CustomerViewModel();
}
public CustomerViewModel CustomerViewModel { get; set; }
}
答案 1 :(得分:0)
请使用以下代码
替换您的UserViewModel Model类公共类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; }
}