How to Have a a Razor Action Link Open in a New Tab

How to have a a razor action link open in a new tab?

Looks like you are confusing Html.ActionLink() for Url.Action(). Url.Action has no parameters to set the Target, because it only returns a URL.

Based on your current code, the anchor should probably look like:

<a href="@Url.Action("RunReport", "Performance", new { reportView = Model.ReportView.ToString() })" 
type="submit"
id="runReport"
target="_blank"
class="button Secondary">
@Reports.RunReport
</a>

open a new tab using MVC Razor @Url.Action

If you are needing to open the URL in a new tab/window, then AJAX is not the correct way to go. You can still open a new tab/window from javascript.

$(document).ready(function () {
$('.callViewSubmit').click(function () {
window.open($(this).parent().data("url"));
});
});

why is @Html.ActionLink opening new tab?

This would happen if you have <base target="_blank">, or if you're holding down Control.

Url.Action download action causes new tab to open/close quickly before download

You are explicitly specifying to open a new tab/page by using target="blank".

From MDN:

target

Specifies where to display the linked URL. It is a name of, or keyword for, a browsing context: a tab, window, or <iframe>.

  • _blank: Load the URL into a new browsing context. This is usually a tab, but users can configure browsers to use new windows instead.

Remove that attribute, and it won't open a new tab/page.

<a href='@Url.Action("ViewFile", "Form", new { id = item.Id })'>
<i class="fa fa-download" aria-hidden="true"></i> @item.Title
</a>

url.Action open link in new window on ASP.NET MVC Page

You don't need to use the helper methods at all:

<a href="http://www.yahoo.com" target="_blank"><span>Go to Yahoo</span></a>

Html.Action is only for controller actions, which an external link is not. There's nothing wrong with using plain HTML to link with.

opening Url.Action in new tab

For it to work you can try :

<button formtarget="_blank" onclick="window.open('@Url.Action("OpenPDF", "FlaggedSurveys", new { id = ViewBag.CompletedCamp } )')" type="button" class="btn btn-primary"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Open Survey PDF </button>

a working example:

<button formtarget="_blank" onclick="window.open('www.google.com')" type="button" class="btn btn-primary"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Open Survey PDF </button>

MVC Open a new tab from tablebutton with data of row

Set the target attribute to _blank, this will cause the link to be opened in a new browser tab.

Pass the ID of the current row as parameter to the Download action, it can load the row data again from DB. Or pass all required data as parameters.

@Html.ActionLink(" ", "Download", "Controller", 
new { @class = "glyphicon glyphicon-pencil",
@title = "Modify",
target ="_blank",
id = row.Id
}
)

See also How to have a a razor action link open in a new tab?

open actionlink in new window instead of new tab

You can try the below code

<a href="#" onclick="openNewWindow();">open</a>

<script type="text/javascript">
function openNewWindow() {
window.open("/values/index", "New Window", "height=500,width=500");
}
</script>

In the url assume Values is your controller and Index as your action

Hope this helps!!

How to open a new tab after server's Post Action Result

at first when user click on print button i post my data by ajax request and after successfully done i open a new tab.

Example:

$.ajax({
url: "@Url.Action("create", "Post")",
type: "POST",
contentType: "application/json",
data: JSON.stringify({ model: model})
}).done(function(result){
window.open('@Url.Action("Print", "controler", new { id = Model.id })', '_blank').focus();
});

OR

You want to write something like your example in http response then you can do something like

  HttpContext.Current.Response.Write( @"<script type='text/javascript' language='javascript'>window.open('page.html','_blank').focus();</script>");

UPDATE

I have added a full flow of a testing project below.

Example:

Model:

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string ProductCode { get; set; }
public decimal Price { get; set; }
}

Controller:

 public class ProductController : Controller
{
// GET: Product
public ActionResult Index()
{
return View();
}


// GET: Product/Create
public ActionResult Save()
{
var model = new Product();
return View(model);
}

// POST: Product/Create
[HttpPost]
public ActionResult Save(Product model, string saveButton)
{
if (ModelState.IsValid)
{
//do something
return
Json(
new
{
redirectTo = Url.Action("Index", "Product", new { Area = "" }),
OpenUrl = Url.Action("Print", "Product", new { Area = "" })

});
}
return View(model);
}
public ActionResult Print()
{
return View();
}
}

Save.cshtml:

@model Product

@{
ViewBag.Title = "Save";
}

<h2>Save</h2>
@Html.Hidden("saveButton","Test")@*Change Test to your value or change it to using JavaScript*@
@using (Html.BeginForm("Save", "Product", new {area = ""}, FormMethod.Post, new {id = "fileForm", name = "fileForm"}))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Product</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ProductCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductCode, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="button" class="btn btn-primary" id="btnSave">Save</button>
<button type="button" class="btn btn-default">Print</button>
</div>
</div>
</div>
}

Script:

<script>
$("#btnSave").click(function() {
$.ajax({
url: $("#fileForm").attr('action'),
type: $("#fileForm").attr('method'),
beforeSend: function() {
},
data: $("#fileForm").serialize() + "&saveButton=" + $("#saveButton").val()
}).done(function(result) {
if (result.OpenUrl) {
window.open(result.OpenUrl, '_blank');
}
if (result.redirectTo) {
setTimeout(function() {
window.location.href = result.redirectTo;
},2000);
}


});
})

</script>


Related Topics



Leave a reply



Submit