2个模型1查看实体框架

时间:2017-09-28 13:33:48

标签: c# asp.net-mvc entity-framework ef-code-first

我正在寻找一种方法,使用Entity Framework在ASP.NET MVC的一个视图中获取2个模型。

我知道互联网上有一些相关信息。 在我的控制器索引方法中,我已经返回了一个视图。

有谁知道如何解决这个问题?

以下是我的模型,视图和控制器:

public class Device
{
    //[Key]
    /// <summary>
    /// 
    /// Unique id of the PC
    /// </summary>
    public int DeviceID { get; set; }

    /// <summary>
    /// The Name of the PC
    /// </summary>
    public string DeviceName { get; set; }

    /// <summary>
    /// The user of the PC
    /// </summary>
    public string UserName { get; set; }
    //public string DeviceMAC { get; set; }
     public IpMac IpMac { get; set; }
}

public class IpMac
{
    [Key]
    /// <summary>
    /// Unique IP of the device
    /// </summary>
    public string DeviceIP { get; set; }
    //[ForeignKey("Device")]
    /// <summary>
    /// Unique ID of the device
    /// </summary>
    public int DeviceID { get; set; }
    /// <summary>
    /// Unique MACAddress of the device
    /// </summary>
    public string DeviceMAC { get; set; }

    //public Device Device { get; set; }
    public ICollection<Device> Device { get; set; }
}

public async Task<IActionResult> Index()
{
    EnumAuthorizationLevel MyLevel = CurrentUserInfo.GetAuthorizationLevel(User);
    ViewBag.AuthorizationLevel = MyLevel;
    return View(await _context.Devices.ToListAsync());
}

我希望你能帮助我,并提前感谢你

0 个答案:

没有答案
相关问题