2012-07-01 37 views
0

我有一個叫Property的類,我只需要在我的列表視圖中顯示幾個項目。我創建了一個Linq查詢來僅返回我的控制器Index方法中的那些項目。我已經實例化試圖返回一個列表Linq查詢到MVC3查看獲取錯誤「有一些無效的參數」

List<Property> props = new List<Property>(); 

控制器的指數方法中,但是當我嘗試「添加」到道具列表「props.Add(getProp);」我收到此錯誤:

「爲System.Collections.Generic.List<PropertyEntities.Property>.Add(PropertyEntities.Property) 的最佳重載的方法匹配具有一些無效參數」

我已經包括了PropertyControler指數方法和屬性類,我在下面的工作:

 public ViewResult Index() 
    { 
     //var properties = db.Properties.Include(p => p.PropertyType); 

     var getProp = from p in db.Properties 
         orderby p.PropName 
         select new 
         { 
          p.PropertyID, 
          p.PropName, 
          p.PropertyStatus, 
          p.City, 
          p.State, 
          p.Bedrooms, 
          p.PropertyType 
         }; 

     List<Property> props = new List<Property>(); 
     props.Add(getProp); 
     return View(props); 
    } 

public partial class Property 
{ 
    //public int temp { get; set; } 

    // Values stored in the view Garage DropDownList 
    public enum GarageType{ None = 0, One = 1, Two = 2, Three = 3, Four = 4, Carport = 5, Other = 6 } 

    public enum PropertyStatusType { Leased = 0, Available = 1, Selling = 2, Sold = 4 } 

    [Required] 
    [ScaffoldColumn(false)] 
    public int PropertyID { get; set; } 

    [Required(ErrorMessage="Generic name for referencing."), 
    StringLength(30, ErrorMessage = "Property name max length is 30 characters."), 
    Display(Name="Property Name", Prompt="Enter Property Name")] 
    public string PropName { get; set; } 

    [Required(ErrorMessage="Select a status for this property."), 
    Display(Name="Property Status")] 
    public int PropertyStatus { get; set; } 

    // used in corolation with the property PropertyStatus 
    // to allow dropdown list to except Enum values 
    // PropertyStatusType 
    public PropertyStatusType PropertyEnumStatus 
    { 
     get { return (PropertyStatusType)PropertyStatus; } 
     set { PropertyStatus = (int)value; } 
    } 

    [Required(ErrorMessage="Select a property type.")] 
    public int PropertyTypeId { get; set; } 

    [Required(ErrorMessage="Address is required."), 
    StringLength(75, ErrorMessage="Address max length is 75 characters.")] 
    public string Address { get; set; } 

    [Required(ErrorMessage = "City name is required."), 
    StringLength(25, ErrorMessage = "City max length is 25 characters.")] 
    public string City { get; set; } 

    [Required(ErrorMessage = "State abbreviation is required."), 
    StringLength(2, ErrorMessage = "State max length is 2 characters.")] 
    public string State { get; set; } 

    [Required(ErrorMessage = "Zip Code is required."), 
    StringLength(5, ErrorMessage = "Zip Code max length is 5 numbers."), 
    Range(00001, 99999)] 
    [Display(Name="Zip Code")] 
    public string ZipCode { get; set; } 

    [Required(ErrorMessage="Square feet is required."), 
    Display(Name="Square Feet")] 
    public int SquareFeet { get; set; } 


    [Required(ErrorMessage = "Number of bedrooms is required."), 
    Range(0,10)] 
    public int Bedrooms { get; set; } 

    [Required(ErrorMessage="Number of bathrooms is required."), 
    Range(0,20)] 
    public int Bathrooms { get; set; } 

    public int Garage { get; set; } 


    // used in corolation with the property Garage 
    // to allow dropdown list to except Enum values 
    // of GarageType 
    [NotMapped] 
    public GarageType GarageEnumValue 
    { 
     get { return (GarageType)Garage; } 
     set{ Garage = (int)value; } 
    } 

    [Display(Name="Morgage Amount"), 
    Range(0.00, 100000000.00)] 
    public Nullable<decimal> MonthlyMortgage { get; set; } 

    [Display(Name="HOA Dues"), 
    Range(0.00, 1000.00)] 
    public Nullable<decimal> HousingDues { get; set; } 

    [Display(Name="Property Tax"), 
    Range(0.0, 100000000.00)] 
    public Nullable<decimal> Tax { get; set; } 

    [Display(Name="Property Insurance"), 
    Range(0.0, 100000000.00)] 
    public Nullable<decimal> Insurance { get; set; } 

    [Display(Name="Assessed Value"), 
    Range(0.0, 100000000.00)] 
    public Nullable<decimal> AssessedValue { get; set; } 

    [Display(Name="Current Value"), 
    Range(0.0, 100000000.00)] 
    public Nullable<decimal> CurrentValue { get; set; } 

    [DataType(DataType.MultilineText)] 
    [StringLength(500, ErrorMessage="You have reached the allotted 500 characters.")] 
    public string Notes { get; set; } 

    public virtual ICollection<Lease> Leases { get; set; } 

    public virtual PropertyType PropertyType { get; set; } 

    public virtual ICollection<Service> Services { get; set; } 
} 
+0

而不是所有的選擇列,你嘗試'select p'在你的查詢?因爲你通過'new'返回的對象不是'Property'對象。 –

+0

@tereško我只是試圖選擇屬性類中找到的屬性的子集。這就是爲什麼我試圖使用新的{p.someprop}來只選擇所需的值。糾正我,如果我錯了聽到,但如果我「選擇p」我會返回屬性實體的每個屬性正確? – Shawn

回答

1

創建一個視圖模型類,當你需要一個實體的屬性的子集,或實體的屬性的子集的混合

public class PropertyViewModel { 
    public int PropertyID {get;set;} 
    public string PropName {get;set;} 
    public int PropertyStatus {get;set;} 
    //etc. 
    public PropertyType PropertyType {get;set;} 
} 

然後選擇每個屬性的新PropertyViewModel(或使用AutoMapper,這將是罰款你的需要https://github.com/AutoMapper/AutoMapper

public ViewResult Index() 
    { 
     var properties = db.Properties.Include(prop => prop.PropertyType) 
       .Select(p => new PropertyViewModel { 
           PropertyID = p.PropertyID, 
           PropName = p.PropName, 
           //etc. 
           }) 
        .ToList(); 
     return View(properties);//View's model should be of tye IList<PropertyViewModel> (or IEnumerable) 
    } 
+0

我曾嘗試創建一個ViewModel,但我有問題返回PropertyStatus的字符串值,如果您返回原始類Properties,則會返回一個名爲PropertyEnumStatus的屬性,該屬性與'public int PropertyStatus {get;組; } //在corolation使用屬性PropertyStatus //允許下拉列表除外枚舉值 // PropertyStatusType 公共PropertyStatusType PropertyEnumStatus { 得到{(PropertyStatusType)PropertyStatus; } set {PropertyStatus =(int)value; } }' – Shawn

+0

問題是返回到視圖的列表是Key值,而不是Enum屬性中的字符串表示形式。 – Shawn

+0

所有....感謝您的幫助,但我覺得我可能需要將我的設計改回到ViewModel概念,例如Raphaël解釋了加載在這些URL的asp.net/mvc/tutorials/javascript上找到的DropDownList的正確方法/ ...和blogs.msdn.com/b/rickandy/archive/2012/01/09/... - RickAndMSFT。 – Shawn

0

很好,它看起來像你正在返回從LINQ查詢匿名類型的IEnumerable<T>,但期待List<Property>接受這些對象作爲類型Property。所以自然Add方法將抵制。

+0

感謝您的迴應,並從那以後,但我可能真的需要的是一個ViewModel下面描述,但它給我的問題,我在後續的帖子中解釋了Raphaël – Shawn