开发者

ASP.NET MVC 2: Return Multiple Objects?

开发者 https://www.devze.com 2023-01-28 14:21 出处:网络
List<Filter> filters; List<Deal> deals; using (DealDataContext db = new DealDataContext()) {
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;

What is the best way of returning more than one object?


Create a ViewModel look for best practices. Your case -

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

Function -

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;

}


Create a class that wraps both objects up into one. Then return your wrapper object.

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

精彩评论

暂无评论...
验证码 换一张
取 消