ASP.NET MVC 2:返回多个对象?

时间:2010-12-03 18:36:05

标签: c# asp.net-mvc-2

List<Filter> filters;
List<Deal> deals;
using (DealDataContext db = new DealDataContext())
{
    XElement xmlTree = XElement.Parse("<Request><ZipCode>92618</ZipCode></Request>");
    var result = db.SearchDeals(xmlTree);

    filters = result.GetResult<Filter>().ToList();
    deals = result.GetResult<Deal>().ToList();

}

return filters, deals;

返回多个对象的最佳方法是什么?

2 个答案:

答案 0 :(得分:4)

ViewModel创建best practices外观。你的情况 -

public class DealViewModel
{
List<Filter> filters{get; set;}
List<Deal> deals{get; set;}
}

功能 -

DealViewModel vm= new DeakViewModel();

using (DealDataContext db = new DealDataContext())
{
    XElement xmlTree = XElement.Parse("<Request><ZipCode>92618</ZipCode></Request>");
    var result = db.SearchDeals(xmlTree);

    vm.filters = result.GetResult<Filter>().ToList();
    vm.deals = result.GetResult<Deal>().ToList();
    return vm;

}

答案 1 :(得分:1)

创建一个将两个对象包装成一个的类。然后返回你的包装器对象。

public class Wrapper
{
   List<Filter> Filters { get; set; }
   List<Deal> Deals { get; set; }
}