m ali
m ali

Reputation: 646

open a new tab using MVC Razor @Url.Action

Am trying to work out how to open a tab when i click a link.

Here is my link which am trying to open, i have set it as this, but as you can see i have added target = "_blank" which does not open in a new tab.

  <div data-callno='@parts.Call_Num' data-url="@Url.Action("GetCallInfo", "CallHandling" , new {target = "_blank"})">
  <div class="callViewSubmit toolbarIcon"></div>
  Test </div>

This opens on the same page, after looking at google i noticed am using URL.ACTION and not HTML.ACTIONLINK.

as a test i tried this on the page, and it did open in a new tab:

    @Html.ActionLink("New report", "New", "Report", null, new {target = "_blank"})

Any ideas how i can open my div in a new tab using the Url.Action way.

//New

This is the javascript i used to open the link, would i able to open the link in javascript rather than url.action

    $(document).ready(function () {
    $('.callViewSubmit').click(function () {
        $.ajax({
            type: "GET",
            url: $(this).parent().data("url"),
            data: { callNumber: $(this).parent().data("callno") },
            success: function (data) {
                $("#CallDetail").html(data);
            },
        });
    });
});

Upvotes: 0

Views: 10208

Answers (3)

Nas
Nas

Reputation: 1

You can use this way:

    Url.RouteUrl("routeName",
new
{
  controller = "controllerName",
  action = "ActionName",
  callNumber: "Value"
});

Then create link:

var link =
                                        "<a data-original-title=\"title\" target='_top' data-toggle=\"tooltip\" data-placement=\"top\" " +
                                        "href=\"" + viewLink +
                                        "\" data-ajax=\"true\" data-ajax-method=\"GET\" data-ajax-mode=\"replace\" " +
                                        "data-ajax-update=\"#CallDetail\" >" + This is link;

Upvotes: 0

Tommy
Tommy

Reputation: 39807

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"));
    });
});

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101604

If you're assigning this in a data-* attribute (and only thing you can modify is the URI, there's nothing you can do to accomplish the behavior. This would need to be changed in the piece of code accessing data-url (and how it handles it).

Upvotes: 1

Related Questions