Search code examples
c#linqasp.net-mvc-5strongly-typed-dataset

Add the navigation property value to strongly typed model data in ASP.NET-MVC-5 using LINQ


I am working on ASP.NET-MVC-5 application. I have model class which has nevigation attribute called PropertyTpe, I need to add the value to it using linq by joining propertyTypeID which is acting in following model as foreign key. I have join in LINQ but i am still missing this value in it, I guess I am missing here something!!!!

Method returning List of strongly typed model

  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; }
    }

model class

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; }

}

Solution

  • I have found answer as following;

    update 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();
    

    and in razor code loop as

     @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>
        }