开发者

Opening a new window using MVC

开发者 https://www.devze.com 2023-01-14 08:30 出处:网络
Basically I want a button that opens a page in a new window containing my http://fake.com/Report/PageFixes... page.Currently I have a button in a form but if there is a better way to do it then I\'m u

Basically I want a button that opens a page in a new window containing my http://fake.com/Report/PageFixes... page. Currently I have a button in a form but if there is a better way to do it then I'm up for that too.

<% using (Html.BeginForm("PageFixes", "Report", new { id = 9 }, FormMethod.Get, new { onSubmit="window.open()" })) %>
<% { %>
    <%= Html.CSButton(new ButtonViewData() { Text = "Submit" })%>
<% } %>开发者_如何学Go;


You don't need to have a 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 wasn't sure how you were getting the 9 for the ID but I assume it is from your model so you could replace the "9" with Model.ID or something.

The key is generating the url with Url.Action and then just using the basic javascript function window.open to open the url in a new browser window.


How about just some javascript on a button like so

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

Or if you would prefer to build the URL dynamically

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


You could still do it the old-fashioned way with an onclick JavaScript function. Here's how I did it, where I needed to pass the ID from the Model to my new URL, which was to another View that was in a folder on 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

精彩评论

暂无评论...
验证码 换一张
取 消