0

List ビューにいくつかのアイテムを表示するだけでよい Property というクラスがあります。コントローラーの Index メソッドでこれらの項目のみを返す Linq クエリを作成しました。インスタンス化しました

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

コントローラーのIndexメソッド内ですが、小道具リストに「追加」しようとすると「props.Add(getProp);」次のエラーが表示されます。

「最適なオーバーロードされたメソッド マッチにSystem.Collections.Generic.List<PropertyEntities.Property>.Add(PropertyEntities.Property) は無効な引数があります」

以下に、PropertyControler インデックス メソッドと、使用している Property クラスを含めました。

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

2 に答える 2

1

エンティティのプロパティのサブセット、またはエンティティのプロパティのサブセットの組み合わせが必要な場合は、ViewModelクラスを作成します

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)
    }
于 2012-07-01T08:15:33.903 に答える
0

IEnumerable<T>まあ、 linq クエリから匿名型の を返しているようですが、 がList<Property>これらのオブジェクトを type として受け入れることを期待していますProperty。当然、Addメソッドは抵抗します。

于 2012-07-01T07:49:05.783 に答える