简体   繁体   English

在JS-asp.net MVC中使用@ Url.Action

[英]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. 我有一个正在创建的项目,并将其发布在IIS中,一切正常,我可以可视化我的Crystal 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. 现在,我已经重新发布了该项目(IIS中的更新),但是看不到从js调用的Crystal Reports报告。

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: 在调用它时,它会打开一个新选项卡(应该是),并显示404:

在此处输入图片说明

When I open another report (from the controller) it shows me the correct path (everything from IIS): 当我打开另一个报告(从控制器)时,它向我显示了正确的path (所有来自IIS的path ):

在此处输入图片说明

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: 正如我在这里读到的,如果我需要使用指向js中的asp.net MVC元素的URL,它们应该与@Url.Action一起使用,所以我尝试以下操作:

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

But when I navigate, I get the URL in this way: 但是,当我导航时,我是通过以下方式获取网址的:

在此处输入图片说明

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? 在两种情况下(Visual Studio和使用JavaScript发布的项目),我如何才能完成路线,还是可以转到视图/控制器?

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

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. 问题是您正在使用包含在Razor视图中的外部JS文件,并且显然您不能使用Razor HTML帮助器在JS文件中呈现完整的URL。 You need to pass the URL as JS function parameter instead: 您需要改为将URL作为JS函数参数传递:

External JS 外部JS

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

Razor view ( <script> tag) 剃刀视图( <script>标签)

$('.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); 
       }
   });
});

What I did to solve it and can help someone: 我为解决该问题所做的工作并可以帮助某人:

Create a div that has nothing, just with a clas s and data-request-ur l in this last I send my @Url.Action() 最后,我将@Url.Action()发送给一个创建没有任何内容的div ,只包含一个clasdata-request-ur l

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

In JS capture the value of the data that is in the div 在JS中捕获div data的值

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

Works for both in Visual Studio and in a published project 在Visual Studio和已发布项目中均适用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 外部 js 文件中的 Asp.Net Mvc Url.Action? - Asp.Net Mvc Url.Action in external js file? 区域 asp.net cor mvc 的 Url.Action - Url.Action for area asp.net cor mvc c# asp.net mvc - 如何将参数传递到 Url.Action - c# asp.net mvc - how to pass parameter into Url.Action ASP.NET MVC和Javascript-提交按钮提交表单,onclick @ URL.Action不触发 - ASP.NET MVC and Javascript - Submit button submitting form, onclick @URL.Action not firing 无法显示带有asp.net mvc url.action链接的引导对话框 - Cannot display bootstrap dialog with an asp.net mvc url.action link 在 ASP.Net MVC JavaScript URL.Action 调用中嵌入表单值 - Embedding form values in an ASP.Net MVC JavaScript URL.Action Call 页面中包含的脚本中带有url.action助手的ASP.NET MVC jQuery自动完成功能 - ASP.NET MVC jQuery autocomplete with url.action helper in a script included in a page 如何使用ASP.NET MVC WebForms视图引擎在url.action中使用javascript变量 - How to use a javascript variable in url.action with ASP.NET MVC WebForms view engine Url.Action '@Url.Action(“Update”, “Admin”)'查询字符串在 .NET MVC 中不起作用 - The Url.Action '@Url.Action(“Update”, “Admin”)'query string which is not working in .NET MVC 将JavaScript变量传递给ASP MVC中的@ Url.Action - Pass javascript variable to @Url.Action in ASP MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM