首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LINQ向ASP.NET-MVC-5中的强类型模型数据添加导航属性值

使用LINQ向ASP.NET-MVC-5中的强类型模型数据添加导航属性值
EN

Stack Overflow用户
提问于 2015-07-31 10:14:02
回答 1查看 227关注 0票数 0

我正在研究ASP.NET-MVC-5应用程序。我有一个模型类,它有一个名为PropertyTpe的嵌套属性,我需要通过将propertyTypeID作为外键加入propertyTypeID来添加它的值。我加入了LINQ,但我仍然错过了这个价值,我想我在这里错过了一些东西!

强类型模型的方法返回列表

代码语言:javascript
复制
  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; }
    }

模型类

代码语言:javascript
复制
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; }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-31 10:23:30

我找到了以下答案;

更新linq;

代码语言:javascript
复制
 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();

在剃须刀代码循环中

代码语言:javascript
复制
 @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>
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31743697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档