简体   繁体   English

从当前视图打印另一个视图

[英]Print another view from present view

How to print different controller/view from present view. 如何从当前视图打印不同的控制器/视图。 There are 2 controllers namely ProductsController and AddressController and views to display Products and Address. 有2个控制器,即ProductsController和AddressController以及用于显示Products和Address的视图。 From Products view, how to print Address view? 在“产品”视图中,如何打印“地址”视图?

public class ProductsController : Controller
    {
        public ActionResult GetProdcuts()
        {
            return View();
        }
    }

public class AddressController : Controller
    {
        public ActionResult GetAddress()
        {
            return View();
        }
    }

GetProducts.cshtml GetProducts.cshtml

<script type="text/javascript">
        function print() {

            var restorepage = document.body.innerHTML;
            var printContent =@Url.Action("GetAddress", "Address");
            document.body.innerHTML = printContent;
            window.print();
            document.body.innerHTML = restorepage;
        }

    </script>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>GetProdcuts</title>
</head>
<body>
    <div>

        <h1>Products Page</h1>


        <a href="#" onclick="javascript:window.print();">Print products</a>
        <br>
        <a href="#" onclick="print()">Print address</a>
    </div>
</body>
</html>

GetAddress.cshml GetAddress.cshml

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>GetAddress</title>
</head>
<body>
    <div> 

        <h1>Address page</h1>
    </div>
</body>
</html>

"Print products" works as expected. “打印产品”按预期工作。 But "Print Address" throws error. 但是“打印地址”会引发错误。

you can create separate view model in which you can add both product and address details and call it in view like: 您可以创建单独的视图模型,在其中可以添加产品和地址详细信息,并在视图中调用它,例如:

@model projectname.ProductAddressViewModel @model项目名.ProductAddressViewModel

and access the elements in that view model 并访问该视图模型中的元素

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

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