用相关信息填充列表

本文关键字:填充 列表 信息 | 更新日期: 2023-09-27 18:01:02

I有一个使用此控制器填充的列表视图:

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

我还想用从另外两个表Day&月

我试过以下线路:

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

但它给出了以下错误:

附加信息:在类型"System.Data.Entity.DbSet1[GRCWebApp.Models.Day]"上声明的方法"GRCWebApp.Models.Day Find(System.Object[]("不能用类型为"System.Data.Ientity.Core.Objects.ObjectQuery1[GRCWebApp.Models.Day]"的实例调用

引用数据的正确方式是什么?

型号为:

会员类型

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

ViewModel是:

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

用相关信息填充列表

您的MembershipType上有DayIdMonthId,但没有public Day Day {get;set;}public Month Month {get;set;} 的属性

如果你有这些属性,你可以使用ReducedDate = t.Day.DayNum + "/" + t.Month.MonthName

这可能会给您一个错误,因为DayNum是一个int,所以您需要将DayNum转换为string。您可以使用SqlFunctions.StringConvert((double)t.Day.DayNum)来执行此操作。