如何将ViewBag作为MVC中的列表传递给View?

时间:2018-05-03 05:08:21

标签: asp.net-mvc

我想在视图中显示2个表,

  1. SalesDocument(单行)
  2. DeliverySchedule(多行/列表)
  3. 我使用ViewBag将值传递给View,代码如下:

    控制器:

    public ActionResult Detail(string id)
    {
            ViewBag.SalesDocument = logisticsContext.Sap_Sales_Order_Report_qas
                                    .Where(x => x.sales_document == id)
                                    .SingleOrDefault();
    
            var ExistingSchedule = sipContext.OrderDeliverySchedules
                                   .Where(o => o.so_no == id)
                                   .OrderBy(o => o.split_date)
                                   .ToList();
    
            ViewBag.DeliverySchedule = ExistingSchedule
                                       .Select(m => new
                                       {
                                           split_date = string.Format("{0:dd-MMM-yyyy}", m.split_date),
                                           m.split_hour,
                                           split_qty = string.Format("{0:N2}", m.split_qty),
                                           m.remarks
                                       }).ToList();
            return View();
    }
    

    查看:

    @model Interisland.Areas.v2.Models.Logistics.Sap_Sales_Order_Report_qas
    @{
        ViewBag.Title = "Detail";
        var SalesDocument = (Interisland.Areas.v2.Models.Logistics.Sap_Sales_Order_Report_qas)ViewBag.SalesDocument;
    
        var DeliverySchedule = (List<Interisland.Areas.v2.Models.Sip.OrderDeliverySchedule>)ViewBag.DeliverySchedule;
    }
    

    但是,当我尝试在视图上显示它时,它返回了一个异常说:

      

    &#39;无法转换类型为&System; System.Collections.Generic.List 1[<>f__AnonymousType4的对象4 [System.String,System.TimeSpan,System.String,System.String]]&# 39;输入&#39; System.Collections.Generic.List`1

    仅在DeliverySchedule上发生。 我尝试调试它,值是正确的,但是在将它转换为视图中的List时失败了。我的代码有什么问题吗?

    非常感谢您提前提供任何帮助

2 个答案:

答案 0 :(得分:1)

您可以/应该使用customModel将数据从控制器传递到这样的视图,而不是使用ViewBag

public class YourCustomModel
{        
    public Sap_Sales_Order_Report_qas SalesDocument { get; set; }
    public List<DeliverySchedule> DeliverySchedule { get; set; }
}

public class DeliverySchedule
{
    public string split_date { get; set; }
    public string split_hour { get; set; }
    public string split_qty { get; set; }
    public string remarks { get; set; }
}

查看

@model Interisland.Areas.v2.Models.Logistics.YourCustomModel
@{        
    Sap_Sales_Order_Report_qas SalesDocument = Model.SalesDocument;    
    List<DeliverySchedule> DeliverySchedule = Model.DeliverySchedule;
}

答案 1 :(得分:1)

这一行明显抛出InvalidCastException,因为你试图将匿名类型从查询结果转换为OrderDeliverySchedule

var DeliverySchedule = (List<Interisland.Areas.v2.Models.Sip.OrderDeliverySchedule>)ViewBag.DeliverySchedule;

假设你声明了这个viewmodel:

public class ViewModel
{
    // other properties

    public List<OrderDeliverySchedule> DeliverySchedule { get; set; }
}

public class OrderDeliverySchedule
{
    public string split_date { get; set; }
    public string split_hour { get; set; }
    public string split_qty { get; set; }
    public string remarks { get; set; }
}

然后你应该在查询结果中返回viewmodel类名,如下所示:

<强>控制器

var model = new ViewModel();

// other stuff

model.DeliverySchedule = ExistingSchedule.Select(m => new OrderDeliverySchedule
                         {
                             split_date = string.Format("{0:dd-MMM-yyyy}", m.split_date),
                             split_hour = m.split_hour,
                             split_qty = string.Format("{0:N2}", m.split_qty),
                             remarks = m.remarks
                         }).ToList();

 return View(model);

查看

@model ViewModel

@{
    var DeliverySchedule = Model.DeliverySchedule;
}

作为旁注,应避免使用ViewBag来传递集合,因为ViewBag内容在运行时已解析并需要额外的强制转换来获取具有正确类型的内容,而是使用强类型的viewmodel属性。