Opening a new window using MVC

Basically, I want a button that opens the page in a new window containing my page http://fake.com/Report/PageFixes.... I currently have a button in the form, but if there is a better way to do this, I am also ready for this.

<% using (Html.BeginForm("PageFixes", "Report", new { id = 9 }, FormMethod.Get, new { onSubmit="window.open()" })) %>
<% { %>
    <%= Html.CSButton(new ButtonViewData() { Text = "Submit" })%>
<% } %>
+3
source share
3 answers

You do not need to have form. Just add the following HTML to your view:

<button type="button" onclick="window.open('<%= Url.Action("PageFixes", "Report", new { id = "9" } ) %>', '_blank'); return false;">submit</button>

I was not sure how you got 9for ID, but I guess this is from your model, so you can replace "9"with Model.IDor something like that.

URL Url.Action, javascript window.open, URL- .

+6

javascript ,

<button type="button" onclick="window.open('http://fake.com/Report/PageFixes/9')">submit</button>

, URL-

<button type="button" onclick="window.open('<%= Request.Url.Scheme + "://"+Request.Url.Authority+Request.ApplicationPath %>Report/PageFixes/9')">submit</button>
+3

You can still do it in the old fashioned way with the onclick JavaScript function. Here is how I did it, where I needed to transfer the identifier from the model to a new URL that was in a different view, which was in a folder in the same directory branch in my project:

<input type="button" id="@Model.Id" data-id="@Model.Id" onclick="openUrl(this)" data-myurl="/OtherBranch/Detail?id=@Model.Id" />

function openUrl(e) {
    var id = e.id;
    var obj = $("[id='"+id+"']");
    var currentUrl = window.location.href;
    var newBranch = obj.attr('data-myurl');
    var newUrl = currentUrl.split('/FirstBranch')[0] + newBranch;
    window.open(newUrl, "", "height=600,width=400,addressbar=no,menubar=no"); // pop-up window -- should probably ensure no master page is used
    //window.location.href = newUrl; // <-- total redirect option
}
0
source

Source: https://habr.com/ru/post/1762381/


All Articles