Baker1562
Baker1562

Reputation: 347

Using @Url.Action in JS-asp.net MVC

I have a project that I have been creating and I publish it in the IIS, there everything is fine, I can visualize my Crystal Reports reports.

Now I have re-published the project (update in IIS), but I can not see my Crystal Reports reports that I call from js.

var url = "/ReporteIngresoVP/RPVistaPreviaIngreso";
window.open(url, "_blank"); 

At the time of calling it, it opens a new tab (as it should be) and shows me a 404:

enter image description here

When I open another report (from the controller) it shows me the correct path(everything from IIS):

enter image description here

As I read out there if I need to use urls that point to asp.net MVC elements from js, they should go with the @Url.Action so I am trying the following:

var url2='@Url.Action("RPVistaPreviaIngreso","CustomerReporteIngresoVP")';
window.open(url2, "_blank"); 

But when I navigate, I get the URL in this way:

enter image description here

How can I complete the route or can I go to the view/controller what I want in both case, Visual Studio and the project published using javascript?

UPDATE

I try to navigate from a click function, I never pass any url, what can I modify?

$('.vpBTN').click(function () {

        //OTHER DATA
        $.ajax({
            url: "setDatosIngreso",
            method: "POST",
            data: {
                factura: datoFactura,
                poliza: datosPoliza,
                fecha: datosFecha,
                via: datosVia,
                bruto: datosBruto,
                neto: datosNeto,
                bultos: datosBultos
            },
            async: false,
            //dataType: "json",
            success: function (respuesta) {
                var url = "IKOR/ReporteIngresoVP/RPVistaPreviaIngreso";
                window.open(url2, "_blank"); 
            }
        });
    });

Upvotes: 1

Views: 3688

Answers (2)

Baker1562
Baker1562

Reputation: 347

What I did to solve it and can help someone:

Create a div that has nothing, just with a class and data-request-url in this last I send my @Url.Action()

<div class="urlPasar" data-request-url="@Url.Action("RPVistaPreviaIngreso","ReporteIngresoVP")"></div>

In JS capture the value of the data that is in the div

var url = $('.urlPasar').data('request-url');
window.open(url, "_blank");

Works for both in Visual Studio and in a published project

Upvotes: 2

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

The problem is you're using external JS file which included inside Razor view, and obviously you cannot use Razor HTML helpers to render complete URL inside JS file. You need to pass the URL as JS function parameter instead:

External JS

// create this function to redirect
function redirectToUrl(url)
{
    window.open(url, "_blank"); 
}

Razor view (<script> tag)

$('.vpBTN').click(function () {
    // other data

    $.ajax({
       url: "setDatosIngreso",
       method: "POST",
       data: {
           factura: datoFactura,
           poliza: datosPoliza,
           fecha: datosFecha,
           via: datosVia,
           bruto: datosBruto,
           neto: datosNeto,
           bultos: datosBultos
       },
       success: function (respuesta) {
           var url = '@Url.Action("RPVistaPreviaIngreso","CustomerReporteIngresoVP")';
           redirectToUrl(url); 
       }
   });
});

Upvotes: 1

Related Questions