当前上下文 cshtml 中不存在获取变量错误?
Getting error for variable does not exist in current context cshtml?
我有以下用于获取错误的代码
$('#Template').click(function () {
var selectedTempleteType = $('#BulkLoadActionDropDownId option:selected').val();
var path = '@Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';
$(this).attr("href", path);
});
显示 "selectedTempleteType" 的错误。
selectedTempleteType
是一个客户端JS变量,你不能在运行服务器端的@Url.Content()
中使用它(并且@Url.Content()
映射URL路径不正确对于查询字符串,请改用 @Url.Action()
)。你应该改变这个:
var path = '@Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';
给这个:
var path = '@Url.Action("DownloadBulkLoadActionTemplate", "Upload")?templateType=' + selectedTempleteType;
或者在 @Url.Action()
中使用占位符,在客户端使用 replace()
:
var path = '@Url.Action("DownloadBulkLoadActionTemplate", "Upload", new { templateType = "xxxx" })';
path = path.replace("xxxx", selectedTempleteType);
我有以下用于获取错误的代码
$('#Template').click(function () {
var selectedTempleteType = $('#BulkLoadActionDropDownId option:selected').val();
var path = '@Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';
$(this).attr("href", path);
});
显示 "selectedTempleteType" 的错误。
selectedTempleteType
是一个客户端JS变量,你不能在运行服务器端的@Url.Content()
中使用它(并且@Url.Content()
映射URL路径不正确对于查询字符串,请改用 @Url.Action()
)。你应该改变这个:
var path = '@Url.Content("~/Upload/DownloadBulkLoadActionTemplate?templateType=" + selectedTempleteType)';
给这个:
var path = '@Url.Action("DownloadBulkLoadActionTemplate", "Upload")?templateType=' + selectedTempleteType;
或者在 @Url.Action()
中使用占位符,在客户端使用 replace()
:
var path = '@Url.Action("DownloadBulkLoadActionTemplate", "Upload", new { templateType = "xxxx" })';
path = path.replace("xxxx", selectedTempleteType);