简体   繁体   English

如何使用强类型数据模型遍历viewBag

[英]How to loop through viewBag with strongly typed data model

I have ViewBag with strongly typed model data, I need to loop through model data in razor, how can i do that. 我有带有强类型模型数据的ViewBag,我需要在剃刀中循环浏览模型数据,我该怎么做。

Razor (need help here) 剃刀(在此需要帮助)

@foreach (var item in ViewBag.PropertyRentingPrise)
{
    //@Html.DisplayFor(item.PropertyRentingPrise)
      ???????????????????????????????????????
}

Controller 控制者

 ViewBag.PropertyRentingPrise = _propertyManagementServices.GetAllPropertyRentingPrice();

Function returning List of records 函数返回记录列表

 public List<PropertyRentingPrice> GetAllPropertyRentingPrice()
    {
        try
        {
            using (var _uow = new PropertiesManagement_UnitOfWork())
            {
                var _records = (from _propertyTypeList in _uow.PropertyRentingPrice_Repository.GetAll()
                                select _propertyTypeList).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; }

}

You need to parse ViewBag object to a List of PropertyRentingPrice. 您需要将ViewBag对象解析为PropertyRentingPrice的列表。

Try this: 尝试这个:

@{
    var m = ViewBag.PropertyRentingPrice as List<PropertyRentingPrice>;
}

@foreach(var item in m)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM