2015-07-05 143 views
0

填充列表我有一個使用該控制器填充一個列表視圖:與相關信息

 public ActionResult ListClubMembershipType(int clubId) 
    { 
     //Populate the list 
     var types = from s in db.MembershipTypes 
        where (s.ClubId == clubId) 
        orderby s.Type 
        select s; 
     ViewBag.typesCount = types.Count(); 
     var model = types.Select(t => new ListMembershipTypeViewModel    
    { 
     Type = t.Type, 
     ClubId = clubId, 
     Cost = t.Cost, 
     ReducedCost = t.ReducedCost, 
     MinAge = t.MinAge, 
     MaxAge = t.MaxAge, 
    }); 

     return PartialView("_ListClubMembershipType", model); 
    } 

我也想在叫ReducedDate與其他兩個表,日拉的數據視圖模型來填充一個變量&個月。

伊夫嘗試了以下線:

  ReducedDate = db.Days.Find(t.DayId).DayNum + "/" + db.Months.Find(t.MonthId).MonthName, 

,但它提供了以下錯誤:

其他信息:方法GRCWebApp.Models.Day查找(System.Object的[])'上類型中聲明'System.Data.Entity.DbSet1 [GRCWebApp.Models.Day]'不能用'System.Data.Entity.Core.Objects.ObjectQuery1 [GRCWebApp.Models.Day]'實例調用'

什麼是正確的方式來引用數據?

的車型有:

MembershipType

public int MembershipTypeId { get; set; } 

[StringLength(150)] 
[Column(TypeName = "nvarchar")] 
public String Type { get; set; } 

[StringLength(350)] 
[Column(TypeName = "nvarchar")] 
public String Description { get; set; } 

[Column(TypeName = "int")] 
public int ClubId { get; set; } 

[Column(TypeName = "decimal")] 
public Decimal Cost { get; set; } 

[Column(TypeName = "decimal")] 
public Decimal ReducedCost { get; set; } 

[Column(TypeName = "int")] 
public int? DayId { get; set; } 

[Column(TypeName = "int")] 
public int? MonthId { get; set; } 

[Column(TypeName = "int")] 
public int MinAge { get; set; } 

[Column(TypeName = "int")] 
public int MaxAge { get; set; } 

[Column(TypeName = "bit")] 
public bool? Dormant { get; set; } 
} 

的日產品型號:

public class Day 
{ 
public int DayId { get; set; } 

[Column(TypeName = "int")] 
public int DayNum { get; set; } 

public virtual ICollection<MembershipType> MembershipTypes { get; set; } 
} 

的月份型號:

public class Month 
{ 
public int MonthId { get; set; } 

[Column(TypeName = "nvarchar")] 
public String MonthName { get; set; } 

public virtual ICollection<MembershipType> MembershipTypes { get; set; } 
} 

而且視圖模型爲:

public class ListMembershipTypeViewModel 
{ 
[Display(Name = "Type")] 
public String Type { get; set; } 

public int ClubId { get; set; } 

[Display(Name = "Full Cost")] 
public Decimal Cost { get; set; } 

[Display(Name = "Reduced Cost")] 
public Decimal ReducedCost { get; set; } 

[Display(Name = "Reduced From")] 
public string ReducedDate { get; set; } 

[Display(Name = "Min Age")] 
public int MinAge { get; set; } 

[Display(Name = "Max Age")] 
public int MaxAge { get; set; } 
} 

回答

1

你有你的MembershipTypeDayIdMonthId,但你沒有如果你有這些屬性,你可以只使用ReducedDate = t.Day.DayNum + "/" + t.Month.MonthName

public Day Day {get;set;}public Month Month {get;set;}

性質,這可能給你一個錯誤由於DayNum是一個int,因此您需要將DayNum轉換爲string。您可以使用SqlFunctions.StringConvert((double)t.Day.DayNum)來執行此操作。

+0

現貨很多謝謝 –