从视图模型显示数据包括多个模型

时间:2018-09-26 09:20:29

标签: view mapping viewmodel

  public ProductProject VmToProductProject() {
        return new ProductProject
        {
            ProjectID = this.ProjectID,
            ProductCode=this.ProductCode,
            AddedDate=this.AddedDate,
            ProductStatus=this.ProductStatus,
        };
    }

    public ProductProject_VM ProductProjectToVm(ProductProject product) {
        return new ProductProject_VM
        {
            ProjectID = product.ProjectID,
            ProductCode = product.ProductCode,
            AddedDate = product.AddedDate,
            ProductStatus = product.ProductStatus
        };
    }

    public ProjectMaintenance VmToProject() {
        return new ProjectMaintenance
        {
            ProjectID = this.ProjectID,
            ProjectDesc=this.ProjectDesc,
            ProjectStatus = this.ProjectStatus,
            ProjectStartDate = this.ProjectStartDate,
            ProjectEndDate=this.ProjectEndDate,
            CreatedDate=this.CreatedDate,
            CreatedUser=this.CreatedUser,
            UpdatedUser=this.UpdatedUser,
            UpdatedDate=this.UpdatedDate
        };
    }

    public ProductProject_VM ProjectToVm(ProjectMaintenance project)
    {
        return new ProductProject_VM
        {
            ProjectID=project.ProjectID,
            ProjectDesc=project.ProjectDesc,
            ProjectStatus=project.ProjectStatus,
            ProjectStartDate=project.ProjectStartDate,
            ProjectEndDate=project.ProjectEndDate,
            CreatedDate=project.CreatedDate,
            CreatedUser=project.CreatedUser,
            UpdatedDate=project.UpdatedDate,
            UpdatedUser=project.UpdatedUser
        };
    }`[HttpGet]
    public ActionResult CreateUpdateProject(string projectID)
    {
        var project = db.Projects.Where(x => x.ProjectID == projectID).FirstOrDefault();
        var productproject = db.ProductProjects.Where(x => x.ProjectID == projectID).FirstOrDefault();

        var vm = from p in db.Projects
                 join pp in db.ProductProjects on p.ProjectID equals pp.ProjectID
                 where p.ProjectID == projectID && pp.ProjectID == projectID
                 select new {p,pp};

        var item = new ProductProject_VM();

        //Get: Update
        if (project != null)
        {
            #region Generate UpdatedUser
            project.UpdatedUser = Session["Username"].ToString();
            #endregion

            #region Generate UpdatedDate
            project.UpdatedDate = DateTime.Now;
            #endregion

            item.ProjectToVm(project);
            item.ProductProjectToVm(productproject);
            item.ProductCollection = db.Products.ToList<Product>();

            return View(item);
        }`

要显示视图模型中已有的两个模型的数据。但是,我做不到。任何其他方式可以实现。 我尝试过的代码:  item = item.ProjectToVm(项目);  item = item.ProductProjectToVm(productproject); 由于第一个“项目”被替换为第二个“项目”,因此只能在一个模型中使用

0 个答案:

没有答案