Asp.net mvc3打印网页

时间:2013-10-31 12:55:08

标签: javascript html css

我正在使用 MVC3 开发weyb应用程序。我想使用打印机打印 MVC视图。这是我的代码。

控制器

 public ActionResult Index()
 {
        CustomerList custList = CreateCustomerList();
        string url = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
        custList.ImageUrl = url + "Content/report.jpg";
        return View("PrintDemo",custList);
 }
 private CustomerList CreateCustomerList()
 {
        return new CustomerList()
            {
                new Customer { Id = 1, Name = "Peter", Address = "St.Los", Place = "USA" },
                new Customer { Id = 2, Name = "Fred", Address = "Flink 9a", Place = "Rotterdam" },
                new Customer { Id = 3, Name = "Sjonnie", Address = "Paternatenplaats 44", Place = "Enkhuizen" },
                new Customer { Id = 4, Name = "Henk", Address = "Wakerdijk 74", Place = "Utrecht" },
                new Customer { Id = 5, Name = "Klaas", Address = "Paternatenplaats 44", Place = "Plaantan" },
                new Customer { Id = 6, Name = "Andre", Address = "Wolbrantskerkweg 90B ", Place = "Los Angeles" },
                new Customer { Id = 7, Name = "Pieter", Address = "Sam van Houtenstraat 191H", Place = "Emmen" },
                new Customer { Id = 8, Name = "Sjohn", Address = "Polostraat, M. 103-II", Place = "Kantens" },
                new Customer { Id = 9, Name = "John", Address = "Paternatenplaats 44", Place = "Leiden" },
                new Customer { Id = 10, Name = "Bruin", Address = "Anthony Spatzierhof 9", Place = "Maasbracht" },
                new Customer { Id = 11, Name = "Sjonnie", Address = "Van Heuven Goedhartlaan 201", Place = "Potters" },
                new Customer { Id = 12, Name = "Lumunon", Address = "Paternatenplaats 44", Place = "Utrecht" },
                new Customer { Id = 13, Name = "Friese", Address = "Burgemeester Roellstr 243-4", Place = "Rotterdam" },
                new Customer { Id = 14, Name = "Hudephol", Address = "Bilhamerstraat 4", Place = "Vlaardingen" },
                new Customer { Id = 15, Name = "Postema", Address = "Mastbos 77", Place = "Schiedam" },
                new Customer { Id = 16, Name = "Scharn", Address = "Marius Bauerstraat 123", Place = "Dordrecht" },
                new Customer { Id = 17, Name = "Wagenmakers", Address = "Pieter Postsingel 16", Place = "Ede" },
                new Customer { Id = 18, Name = "Gedikli", Address = "Burgemeester Hogguerstr 283", Place = "Twello" },
                new Customer { Id = 19, Name = "Zwollo", Address = "Meervalweg 140", Place = "Terschelling" },
                new Customer { Id = 20, Name = "Sjonnie", Address = "Ruys de Beerenbrouckstr 79A", Place = "Ter Aar" },
                new Customer { Id = 21, Name = "Schimmelmann", Address = "Ritzema Bosstraat 28-2", Place = "Vierenman" },
                new Customer { Id = 22, Name = "Makhlouf", Address = "Ln vd Helende Meesters 12", Place = "Eindhoven" },
                new Customer { Id = 23, Name = "Meyer", Address = "Burgemeester v Leeuwenln 79H", Place = "Breda" },
            };
}

这是我的视图

@using MvcReportGeneratorDemo.Models
@model CustomerList

@{
<script type="text/javascript">
   function PrintDiv() {
       var divToPrint = document.getElementById('printarea');
       var popupWin = window.open('', '_blank', 'width=auto,height=auto,location=no,left=200px');
       popupWin.document.open();
       popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
       popupWin.document.close();
   }
</script>
}
<div id="printarea">
<br/>
<img src="@Model.ImageUrl" width="64" height="64" /> 
<br/>
<table cellpadding="3" cellspacing="3">
    <tr border="1" bgcolor="#777777" color="#ffffff">
        <td width="20%" align="center">
            Name
        </td>
        <td width="50%" align="center">
            Address
        </td>
        <td width="30%" align="center">
            Place
        </td>
    </tr>
    @{
        var OddColor = "#DDEEEE";
        var evenColor = "#EEFFFF";
        var Odd = OddColor;
    }
    @foreach (Customer customer in Model)
    {
        <tr border="1" bgcolor="@Odd">
            <td>@customer.Name
            </td>
            <td>@customer.Address
            </td>
            <td>@customer.Place
            </td>
        </tr>
        if (Odd == OddColor)
        {
            Odd = evenColor;
        }
        else
        {
            Odd = OddColor;
        }
    }
</table>

当我运行此代码时,它以彩色格式向我显示表格。如下 enter image description here

但是当我打印它时,它不会显示颜色的行。它没有显示颜色标题。我该怎么做才能显示颜色?

0 个答案:

没有答案