2

在 ASP MVC Api 上,我有树命名函数来检索数据,但我想将它们组合到一个请求 StylesAll:{},这是代码。

[ActionName("Styles")]
public IEnumerable<StyleDTO> GetStyles()
{
    return from s in db.styles
           select new StyleDTO() { Name = s.Name, StyleId = s.StyleId };

}


[ActionName("Labels")]
public IEnumerable<LabelDTO> GetLabels()
{
    return from l in db.Labels
           select new LabelDTO() { Name = l.Name, LabelId = l.LabelId , image = l.image};
}

[ActionName("Commodity")]
public IEnumerable<CommodityDTO> GetCommodity()
{
    return from c in db.Commodities
           select new CommodityDTO() { CommodityId = c.CommodityId, CreateDate = c.CreateDate, Name = c.Name, Varieties = ( from v in c.Varieties select new VarietyDTO() 
           {
                CommodityId = v.CommodityId, Name = v.Name, VarietyId = v.VarietyId
           }) };
}
4

1 回答 1

3

创建一个 ViewModel 以包含所有数据,然后创建函数来填充 ViewModel 并返回它。

查看模型

public class AllStyles
{
    public IEnumerable<StyleDTO> Commodities {get;set;}
    public IEnumerable<LabelDTO> Labels {get;set;}
    public IEnumerable<CommodityDTO> Styles {get;set;}
}

API 动作方法

[ActionName("StylesAll")]
public AllStyles GetAllStyles()
{
    return new AllStyles{
        Styles = from s in db.styles 
                 select new StyleDTO() { 
                     Name = s.Name, 
                     StyleId = s.StyleId },
        Labels = from l in db.Labels 
                 select new LabelDTO() { 
                     Name = l.Name, 
                     LabelId = l.LabelId , 
                     image = l.image},
        Commodities = from c in db.Commodities 
                      select new CommodityDTO() { 
                          CommodityId = c.CommodityId, 
                          CreateDate = c.CreateDate, 
                          Name = c.Name, 
                          Varieties = ( from v in c.Varieties 
                                        select new VarietyDTO() {
                                            CommodityId = v.CommodityId, 
                                            Name = v.Name, 
                                            VarietyId = v.VarietyId})}
    };
}
于 2013-11-03T22:41:26.567 回答