【问题标题】:Add the navigation property value to strongly typed model data in ASP.NET-MVC-5 using LINQ使用 LINQ 将导航属性值添加到 ASP.NET-MVC-5 中的强类型模型数据
【发布时间】:2015-07-31 10:14:02
【问题描述】:

我正在开发 ASP.NET-MVC-5 应用程序。我有一个模型类,它具有名为 PropertyTpe 的导航属性,我需要使用 linq 通过加入在以下模型中作为外键作用的 propertyTypeID 将值添加到它。我已经加入了 LINQ,但我仍然缺少这个值,我想我在这里缺少一些东西!!!!

方法返回强类型模型列表

  public List<PropertyRentingPrice> GetAllPropertyRentingPrice()
    {
        try
        {
            using (var _uow = new PropertiesManagement_UnitOfWork())
            {
                var _records = (from _propertyRentingPriseList in _uow.PropertyRentingPrice_Repository.GetAll()
                                join _propertyTypeList in _uow.PropertyType_Repository.GetAll() on _propertyRentingPriseList.PropertyTypeID equals _propertyTypeList.PropertyTypeID
                                select _propertyRentingPriseList).ToList();

                return _records;
            }


        }
        catch { return null; }
    }

模型类

public class PropertyRentingPrice
{
     public PropertyRentingPrice() { }


     [Key]
     [Column(Order = 0)]
     [Display(Name = "Property Renting ID")]
     public int RentingID { get; set; }

     [Key][Column(Order=1)]
     [Display(Name = "Housing Organization ID")]
     [Required(ErrorMessage = "Require Housing Organization ID")]
     public int HousingOrganizationID { get; set; }

     [Key]
     [Column(Order = 2)]
     [Display(Name = "Property Type ID")]
     [Required(ErrorMessage = "Require Property Type ID")]
     public int PropertyTypeID { get; set; }

     [Display(Name = "Cost Per Week")]
     [Required(ErrorMessage = "Require Cost Per Week Based on Property Type")]
     public decimal CostPerWeek { get; set; }

     [Display(Name = "Cost Per Month")]
     [Required(ErrorMessage = "Require Cost Per Month Based on Property Type")]
     public decimal CostPerMonth { get; set; }

     [Display(Name = "Currency")]
     [Required(ErrorMessage = "Require Currency In Which Property Been Advertised, Type £ for British Pound")]
     public string Currency { get; set; }

     [Display(Name = "Maximum Occupancy")]
     [Required(ErrorMessage = "Require Maximum Number Occupancy Based on Property Type")]
     public int MaximumOccupancy { get; set; }

     [Display(Name = "Bill Includes")]
     [Required(ErrorMessage = "Require Yes/No if Property Rent Includes Bills")]
     public bool Bill_Includes { get; set; }

     public HousingOrganization HousingOrganization { get; set; }
     public PropertyType PropertyType { get; set; }

}

【问题讨论】:

    标签: c# linq asp.net-mvc-5 strongly-typed-dataset


    【解决方案1】:

    我找到了如下答案;

    更新 linq;

     var _records = (from _propertyRentingPriseList in _uow.PropertyRentingPrice_Repository.GetAll()
                                    join _propertyTypeList in _uow.PropertyType_Repository.GetAll() on _propertyRentingPriseList.PropertyTypeID equals _propertyTypeList.PropertyTypeID
                                    select _propertyRentingPriseList).Include(x=>x.PropertyType).ToList();
    

    在 razor 代码循环中作为

     @foreach (var item in Model._PropertyRentingPriceModel)
        {
            <tr>
                <td>@item.PropertyTypeID</td>
                <td>@item.RentingID</td>
                <td>@item.PropertyType.Type</td>
                <td>@item.CostPerWeek</td>
                <td>@item.CostPerMonth</td>
                <td>@item.Currency</td>
                <td>@item.MaximumOccupancy</td>
                <td>@item.Bill_Includes</td>
            </tr>
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      相关资源
      最近更新 更多