在Viewmodel

时间:2015-06-19 08:40:06

标签: c# asp.net-mvc mvvm

试图理解在视图模型中使用外键关系,但努力使其随处可见。我们有三个表格tblInventorytblLocationstblEquipment

广告资源有LocationID,它是tblLocationEquipmentID的外键,链接到tblEquipment

LocationID的外键应显示“位置名称”的属性,而EquipmentID应显示“设备类型”。

对于单个表,我可以轻松地完成控制器,但是,在尝试通过外键执行此操作时,我只能获取每个属性的ID号。

以下是广告资源视图模型

public class InventoryIndexViewModel
{
    public int InventoryID { get; set; }
    public string StationReference { get; set; }
    public int EquipmentID { get; set; }
    public long LocationID { get; set; }

    public List<tblLocation> Locations { get; set; }
    public List<tblEquipment> Equipment { get; set; }
}

位置 viewmodel:

public class LocationViewModel
{
    public int LocationID { get; set; }
    public string Location { get; set; }
}

设备 viewmodel:

public class EquipmentViewModel
{
    public int EquipmentID { get; set; }
    public string Equipment { get; set; }
}

然后是控制器

    public ActionResult Index()
    {
        var TheInventory = InventoryContext.tblInventories.ToList();

        List<InventoryIndexViewModel> viewModelList = TheInventory
            .Select(u => new InventoryIndexViewModel
            {
                InventoryID = u.InventoryID,
                StationReference = u.StationReference,
                LocationID = u.tblLocation.LocationID,
                EquipmentID = u.tblEquipment.EquipmentID
            }).ToList();
        return View(viewModelList);
    }

我确定我正在做的事情要么是里程数,要么关闭,但不是那里。这是我连接视图模型的方式吗?或者控制器是否完全脱离了点?

由于

0 个答案:

没有答案