【发布时间】:2018-05-03 05:08:21
【问题描述】:
我想在视图中显示 2 个表格,
- 销售文档(单行)
- DeliverySchedule(多行/列表)
我使用 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;
}
但是,当我尝试在视图上显示它时,它返回了一个异常:
'无法将'System.Collections.Generic.List
1[<>f__AnonymousType44[System.String,System.TimeSpan,System.String,System.String]]'类型的对象转换为'System.Collections.Generic。列表`1
这只发生在 DeliverySchedule 上。 我尝试调试它,并且值是正确的,但是在将其转换为视图中的列表时它失败了。我的代码有什么问题吗?
非常感谢您提前提供的任何帮助
【问题讨论】:
-
您正在创建一组匿名对象。您需要创建一个模型并将您查询到该模型的集合的项目 -
.Select(m => new OrderDeliverySchedule{ .... }但无论如何您都不应该使用ViewBag。创建一个包含您需要的所有数据的视图模型并返回该视图模型的实例
标签: asp.net-mvc