当前上下文cshtml中不存在变量获取错误?

时间:2018-10-10 10:34:32

标签: asp.net-mvc-4 razor

我有以下错误代码

$('#Template').click(function () {
            var selectedTempleteType = $('#BulkLoadActionDropDownId option:selected').val();
            var path = '@Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';
            $(this).attr("href", path);
        });

显示“ selectedTempleteType”错误。

1 个答案:

答案 0 :(得分:0)

selectedTempleteType是客户端JS变量,您不能在运行服务器端的@Url.Content()内使用它(并且@Url.Content()不能正确地映射带有查询字符串的URL路径,请使用{ {1}})。您应该对此进行更改:

@Url.Action()

对此:

var path = '@Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';

或在客户端的var path = '@Url.Action("DownloadBulkLoadActionTemplate", "Upload")?templateType=' + selectedTempleteType; @Url.Action()内部使用占位符:

replace()