簡體   English   中英

在asp.net中調用url.action時,模型為null

[英]Model is null when url.action is called in asp.net

我的模型具有從數據庫中獲取的列表產品。 加載索引視圖時,我從用戶輸入中獲取變量,然后模型獲取變量,然后返回視圖OutputTableAsync。 視圖加載后,我單擊鏈接“導出Excel”,在調試模式下,列表產品為空。

我的觀點:

<body>
    @if (Model != null)
    {
        var products = Model.products.Products;
        <a href="@Url.Action("ExportToExcel","Home", new {products = products })">Export Excel</a>
        <table border="1" style="width: 100%">
            <thead>
                <tr>
                    <th>Model</th>
                    <th>Price</th>
                    <th>Link</th>
                    <th>ImageUrl</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var product in Model.products.SortProduct())
                {
                    <tr id="i">
                        <td align="center">@product.Model</td>
                        <td align="center">@product.FormatPrice()</td>
                        <td align="center"><a href="@product.Link">@product.Link</a></td>
                        <td align="center"><img src="@product.ImageUrl" style="width : 200px; height:200px ;" id="@product.ImageUrl" /></td>
                    </tr>
                }
            </tbody>
        </table>
    }
</body>

控制器中的ExportToExcel方法:

public void ExportToExcel(List<Product> products)
{
    ExcelPackage pck = new ExcelPackage();
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Products");
    ws.Cells["A1"].Value = "Model";
    ws.Cells["B1"].Value = "Price";
    ws.Cells["C1"].Value = "Link";
    ws.Cells["D1"].Value = "ImageLink";
    int rowstart = 2;
    foreach(var item in products)
    {
        ws.Cells[string.Format("A{0}", rowstart)].Value = item.Model;
        ws.Cells[string.Format("B{0}", rowstart)].Value = item.Price;
        ws.Cells[string.Format("C{0}", rowstart)].Value = item.Link;
        ws.Cells[string.Format("D{0}", rowstart)].Value = item.ImageUrl;
        rowstart++;
    }
    ws.Cells["A:AZ"].AutoFitColumns();
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment: filename=" + "ExcelFile.xlsx");
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.End();
}

根據此處接受的答案您不能通過Url.Action傳遞復雜類型

在這種情況下,我將傳遞對象的序列化版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM