简体   繁体   中英

ASP.NET Core MVC is possible to open a view in a new tab without using the controller?

For example: I have a View returned by the controller, that receives a object, in this view I have a button to open a new view with the same object as the view that calls it.

If I use Html.RenderPartial inside a hidden div, I can make upon button click the new View open with the caller view model, without using the controller.

What I want is to instead displaying the new view inside a hidden container, if it is possible that on button click to display the View on a new tab??

Best Regards

My idea is still to put this PartialView in a hidden div, then use the window.open method to open a new tab and put this div in it. Below is a simple test:

Main View

@model User

<div id="partialview" style="display: none">
    @{await Html.RenderPartialAsync("MyPartialView", Model);}
</div>

<input type="button" id="btn" value="NewTab" />

@section scripts{ 

    <script>
    $("#btn").click(function () {
        var newTab = window.open("", "_blank", "", true);
        newTab.document.body.innerHTML = $("#partialview").html();
    })
    </script>

}

Partial View

@model User

<h3>User</h3>

<div>
    @Html.DisplayNameFor(m => m.Id)
    <input asp-for="@Model.Id" />
</div>
<div>
    @Html.DisplayNameFor(m => m.UserName)
    <input asp-for="@Model.UserName" />
</div>

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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