简体   繁体   English

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

[英]How to pass ViewBag to View as a list in MVC?

I have 2 tables I want to display in the View, 我有2个表格要显示在“视图”中,

  1. SalesDocument (Single row) SalesDocument(单行)
  2. DeliverySchedule (Multiple row / List) DeliverySchedule(多行/列表)

I am passing the values to View using ViewBag, with code as follow: 我使用ViewBag将值传递给View,代码如下:

Controller: 控制器:

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();
}

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;
}

However, when I am trying to display it on the view, it returned an exception saying: 但是,当我试图在视图上显示它时,它返回了一个异常,说:

'Unable to cast object of type 'System.Collections.Generic.List 1[<>f__AnonymousType4 4[System.String,System.TimeSpan,System.String,System.String]]' to type 'System.Collections.Generic.List`1 '无法转换类型为'System.Collections.Generic.List 1[<>f__AnonymousType4 4 [System.String,System.TimeSpan,System.String,System.String]]的对象”以键入'System.Collections.Generic.List `1

Which happens only on the DeliverySchedule. 仅在DeliverySchedule上发生。 I tried debugging it, and the values were right, however it failed while converting it into a List in the view. 我尝试调试它,并且值正确,但是在将其转换为视图中的List时失败。 Is there anything wrong with my code? 我的代码有什么问题吗?

Thank you very much for any help in advance 非常感谢您的提前帮助

Instead of using ViewBag , you can/should use customModel to Pass the data from controller to view like this, 而不是使用的ViewBag ,你可以/应该使用customModel来传递数据,从控制器查看这个样子,

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; }
}

VIEW 视图

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

This line obviously throwing InvalidCastException because you're trying to cast anonymous type from query result to OrderDeliverySchedule : 该行显然会InvalidCastException因为您正试图将匿名类型从查询结果转换为OrderDeliverySchedule

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

Assumed that you declared this 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; }
}

Then you should returning viewmodel class name inside query results like this: 然后,您应该在查询结果内返回viewmodel类名称,如下所示:

Controller 控制者

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);

View 视图

@model ViewModel

@{
    var DeliverySchedule = Model.DeliverySchedule;
}

As a side note, using ViewBag to pass collections should be avoided because ViewBag contents resolved in runtime and requires extra cast to get the content with proper type, use strongly-typed viewmodel properties instead. 附带说明一下,应避免使用ViewBag传递集合,因为ViewBag内容在运行时已解析,并且需要额外的转换才能获取具有正确类型的内容,请改用强类型的viewmodel属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM