2016-10-02 70 views
0

我想显示我在表格中引用的城市的名称,但我不能AspNetUser如何。 这是IndexViewModels:如何在我的表格中显示引用的名称AspNetUsers

public class IndexViewModel { 
    public bool HasPassword { get; set; } 
    public IList<UserLoginInfo> Logins { get; set; } 
    public string PhoneNumber { get; set; } 
    public bool TwoFactor { get; set; } 
    public bool BrowserRemembered { get; set; } 

    public string Nombre { get; set; } 
    public string Apellido { get; set; } 
    public string Direccion { get; set; } 
    public string Pais { get; set; } 
    public int LocalidadID { get; set; } 
    public string CodigoPostal { get; set; } 
    public string Telefono { get; set; } 
    public virtual Localidad Localidad { get; set; } 
} 

这是类ApplicationUser:

public class ApplicationUser : IdentityUser { 
    public int LocalidadID { get; set; } 
    public string Nombre { get; set; } 
    public string Apellido { get; set; } 
    public string Direccion { get; set; } 
    public string Pais { get; set; }   
    public string CodigoPostal { get; set; }   
    public string Telefono { get; set; } 
    public System.DateTime FechaRegistro { get; set; } 

    // FOREIGN KEY 
    public virtual Localidad Localidad { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

这是控制器指数IdentityModels

public async Task<ActionResult> Index(ManageMessageId? message) 
    { 

     if (User.Identity.Name.Equals("[email protected]")) 
      return RedirectToAction("GuestIndex"); 

     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
     var store = new UserStore<ApplicationUser>(new ApplicationDbContext()); 
     var ctx = store.Context; 
     var usuario = manager.FindById(User.Identity.GetUserId()); 
     ApplicationDbContext db = new ApplicationDbContext(); 

     var model = new IndexViewModel 
     { 
      HasPassword = HasPassword(), 
      PhoneNumber = await UserManager.GetPhoneNumberAsync(User.Identity.GetUserId()), 
      TwoFactor = await UserManager.GetTwoFactorEnabledAsync(User.Identity.GetUserId()), 
      Logins = await UserManager.GetLoginsAsync(User.Identity.GetUserId()), 
      BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(User.Identity.GetUserId()), 
      Direccion = usuario.Direccion, 
      Nombre = usuario.Nombre, 
      Apellido = usuario.Apellido, 
      Telefono = usuario.Telefono, 
      LocalidadID = usuario.LocalidadID, 
      //Localidades = db.Localidades.Where(l => l.LocalidadID==usuario.LocalidadID).Select(l => new {Nombre = l.Nombre}), 
      CodigoPostal = usuario.CodigoPostal 
     };    
     return View(model); 
    } 

而我查看我把我的错误:

<p>Argentina, @Model.Localidad.Departamento.Provincia.Nombre</p> 

错误:未将对象引用设置为对象的实例。

+0

什么是'Localidad'? – Hadee

+0

Localidad = city,Departamento = department,Provincia = State。 :-) – Max

回答

0

LocalidadDepartamento.Provincia将不会被加载,也可能是Localidad本身。

你必须使用Include从上下文加载或显式装载它在另一个查询 例如:

LocalidadID = db.Localidads.Include("Departamento.Provincia.").Where(a=>a.LocalidadId == usuario .LocalidadID) 
+0

谢谢,但抛出以下错误: 不能隐式地将类型'System.Linq.IQueryable '转换为'int' – Max

+0

非常感谢!我牧养,我用这种方式,因为我看到了转换错误,而不是分配FOREIGN KEY“LocalidadID”分配给LOCALITY模型。 Localidad =(从db.Localidades中的l where l.LocalidadID == usuario.LocalidadID select l).First (), – Max

1

解决的办法是添加以下行:

Localidad = (from l in db.Localidades where l.LocalidadID == usuario.LocalidadID select l).First<Localidad>() 

离开功能如下:

public async Task<ActionResult> Index(ManageMessageId? message) 
{ 

    if (User.Identity.Name.Equals("[email protected]")) 
     return RedirectToAction("GuestIndex"); 

    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
    var store = new UserStore<ApplicationUser>(new ApplicationDbContext()); 
    var ctx = store.Context; 
    var usuario = manager.FindById(User.Identity.GetUserId()); 
    ApplicationDbContext db = new ApplicationDbContext(); 

    var model = new IndexViewModel 
    { 
     HasPassword = HasPassword(), 
     PhoneNumber = await UserManager.GetPhoneNumberAsync(User.Identity.GetUserId()), 
     TwoFactor = await UserManager.GetTwoFactorEnabledAsync(User.Identity.GetUserId()), 
     Logins = await UserManager.GetLoginsAsync(User.Identity.GetUserId()), 
     BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(User.Identity.GetUserId()), 
     Direccion = usuario.Direccion, 
     Nombre = usuario.Nombre, 
     Apellido = usuario.Apellido, 
     Telefono = usuario.Telefono, 
     LocalidadID = usuario.LocalidadID, 
     Localidad = (from l in db.Localidades where l.LocalidadID == usuario.LocalidadID select l).First<Localidad>(), 
     CodigoPostal = usuario.CodigoPostal 
    };    
    return View(model); 
} 
+0

Ready! ........ – Max

相关问题