1

我想将以下两个模型类与控制器中的实体框架结合起来,以便在视图中显示会计系统中的当前因素

<pre>
namespace AccountingSystem.Models
{
    public class BuyFactor
    {
        public int BuyFactorId { get; set; }        
        public DateTime CreateDate { get; set; }        
        public string Seller { get; set; }
        public string Creator { get; set; }
        public decimal SumAllPrice { get; set; }
        public ICollection<BuyFactorDetail> BuyFactorDetails { get; set; }
    }
}

namespace AccountingSystem.Models
{
    public class BuyFactorDetail
    {
        public int BuyFactorDetailId { get; set; }         
        public int Number { get; set; }
        public string Description { get; set; }
        public decimal SumPrice { get; set; }
        public int BuyFactorId { get; set; }
        public virtual BuyFactor BuyFactor { get; set; }        
        public virtual Commodity Commodity { get; set; }        
    }
}
</pre>
4

6 回答 6

1

创建一个新模型

public class JointModel
{
   public BuyFactor BuyFactor {get; set;}
   public BuyFactorDetail BuyFactorDetail {get; set;}
}
于 2013-08-01T05:15:14.503 回答
1

只需创建另一个模型,然后在其中调用另一个模型

 public class ParentModel
 {
 public BuyFactor BuyFactor {get; set;}
 public BuyFactorDetail BuyFactorDetail {get; set;}
 }

所以当你在视图中调用它时

 @model IEnumerable<AccountingSystem.Models.ParentModel>

   @Html.DisplayNameFor(model => model.BuyFactor.Creator)
于 2017-03-15T11:43:51.087 回答
0

使用 Linq 连接查询,例如

    var query = (from b in context.BuyFactors
                     join d in context.BuyFactorDetail
                     on
..
                     select new
                     {
                        BuyFactorId = b.BuyFactorId,
                        ....
                        BuyFactorDetailId = d.BuyFactorDetailId,
                        ...

    ..
                 }));
于 2013-08-01T06:24:19.553 回答
0

您的 BuyFactor 已包含 BuyFactorDetail 集合。您确定实体之间是 1:N 关系吗?您可以使用 BuyFactor 作为模型,并且可以使用 BuyFactor 实体的 BuyFactorDetails 属性。

于 2013-08-01T06:29:15.893 回答
0

在控制器中使用 ViewBag 为两者分配各自的对象。

ViewBag.BuyFactor= BuyFactor;
ViewBab.BuyFactorDetail = BuyFactorDetail;

要在视图中使用它,您必须将它们类型转换回来。

于 2013-09-30T13:00:27.507 回答
0

在主模型中定义Model为 a的最佳方式Property

例如

 public class BuyFactor
{
    public int BuyFactorId { get; set; }        
    public DateTime CreateDate { get; set; }        
    public string Seller { get; set; }
    public string Creator { get; set; }
    public decimal SumAllPrice { get; set; }
    public ICollection<BuyFactorDetail> BuyFactorDetails { get; set; }
    public BuyFactorDetail BuyFactorEntity {get;set;}
}

赋值BuyFactorEntity并用作Model.BuyFactorEntity.BuyFactorDetailId

于 2013-08-01T06:18:37.270 回答