用相关信息填充列表 [英] Populating List with related information

查看:57
本文介绍了用相关信息填充列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I Have a list view that is populated using this controller:

        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& amp;中提取的数据填充一个称为ReducedDate的变量.月.

I would also like to populate a variable in the view model called ReducedDate with data pulled from two other tables, Day & Month.

我尝试过以下行:

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

但是它给出了以下错误:

but it gives the following error:

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

Additional information: Method 'GRCWebApp.Models.Day Find(System.Object[])' declared on type 'System.Data.Entity.DbSet1[GRCWebApp.Models.Day]' cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery1[GRCWebApp.Models.Day]'

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

What is the correct way to reference the data?

模型是:

会员类型

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是:

And the ViewModel is:

    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 上具有 DayId MonthId ,但是您没有具有 public Day Day {get; set;} public Month Month {get; set;}

You have DayId and MonthId on your MembershipType but you don't have properties of public Day Day {get;set;} and public Month Month {get;set;}

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

If you had those properties you could just use ReducedDate = t.Day.DayNum + "/" + t.Month.MonthName

这可能会给您带来错误,因为 DayNum 是一个整数,因此您需要将 DayNum 转换为 string .您可以使用 SqlFunctions.StringConvert((double)t.Day.DayNum)来做到这一点.

This might give you an error since DayNum is an int so you'll need to convert DayNum to a string. You can use SqlFunctions.StringConvert((double)t.Day.DayNum) to do this.

这篇关于用相关信息填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆