名称';id';在Javascript方法的当前上下文中不存在
通过尝试以下几种方法,我无法在lambda表达式中使用传递给javascript方法的参数。如何在下面的表达式中使用id参数?提前谢谢 文件名中有一个超链接,我成功地将ID参数传递给Javascript方法:名称';id';在Javascript方法的当前上下文中不存在,javascript,jquery,asp.net-mvc,lambda,url-parameters,Javascript,Jquery,Asp.net Mvc,Lambda,Url Parameters,通过尝试以下几种方法,我无法在lambda表达式中使用传递给javascript方法的参数。如何在下面的表达式中使用id参数?提前谢谢 文件名中有一个超链接,我成功地将ID参数传递给Javascript方法: <a onclick="downloadFile(@p.ID);">@p.FileName</a> function downloadFile(id) { $.fancybox({ //This works: (p.ID == 82)
<a onclick="downloadFile(@p.ID);">@p.FileName</a>
function downloadFile(id) {
$.fancybox({
//This works: (p.ID == 82)
content: '<img src="data:image/png;base64,@System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(p => p.ID == 82 ).FileData)" alt=""/>',
//They are not works: (p.ID == id / p.ID == @id / p.ID == this.id)
content: '<img src="data:image/png;base64,@System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(p => p.ID == id ).FileData)" alt=""/>',
content: '<img src="data:image/png;base64,@System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(p => p.ID == @id ).FileData)" alt=""/>',
content: '<img src="data:image/png;base64,@System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(p => p.ID == this.id ).FileData)" alt=""/>',
type: "html"
});
}
@p.FileName
函数下载文件(id){
$.fancybox({
//这是有效的:(p.ID==82)
内容:'p.ID==82.FileData)“alt=”“/>”,
//它们不是作品:(p.ID==ID/p.ID==@ID/p.ID==this.ID)
内容:“p.ID==ID).FileData)“alt=”“/>”,
内容:'p.ID==@ID.FileData)“alt=”“/>',
内容:“p.ID==this.ID).FileData)“alt=”“/>”,
键入:“html”
});
}
更新:这是我以前使用的Ajax方法
function downloadFile(id) {
$.ajax({
url: "/Issue/RenderImage",
type: "POST",
data: JSON.stringify({ 'id': id }),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (response) {
$.fancybox({
content: '<img height="200" width="250" src="data:image/png;base64,' + response.Image + '" />',
type: "html"
//});
},
error: function () {
alert("an error has occured!!!");
}
});
}
函数下载文件(id){
$.ajax({
url:“/Issue/RenderImage”,
类型:“POST”,
数据:JSON.stringify({'id':id}),
数据类型:“json”,
传统的:是的,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(响应){
$.fancybox({
内容:“”,
键入:“html”
//});
},
错误:函数(){
警报(“发生错误!!!”;
}
});
}
如果有人告诉您不需要ajax,那么图像数据似乎可以发布到页面,就像字典一样,您可以更改代码,并将您的模型.FileAttachments
作为js的一部分,就像
var allPictures= @JsonConvert.SerializeObject(Model.FileAttachments.ToDictionary(k=>k.ID,v=>System.Convert.ToBase64String(v.FileData)));
function downloadFile(id) {
$.fancybox({
content: '<img src="data:image/png;base64,'+allPictures[id] +'" alt=""/>',
type: "html"
});
}
var allPictures=@JsonConvert.SerializeObject(Model.FileAttachments.ToDictionary(k=>k.ID,v=>System.Convert.ToBase64String(v.FileData));
函数下载文件(id){
$.fancybox({
内容:“”,
键入:“html”
});
}
而你说你想下载其他类型的文件(pdf等),它不能像图片一样,你可以使用这些代码,如果没有下载文件的限制
<a href="filePath">fileName</a>
我通过修改@Sky Fang的答案发布了代码的最终工作状态,以便有人需要使用它。我还将title参数传递给javascript方法 查看:
@using Newtonsoft.Json
<a onclick="showImage(@p.ID, '@p.FileName - @p.Created - @p.AuthorID');">@p.FileName</a>
<script>
function showImage(id, imageTitle) {
$.fancybox.showLoading(); //Shows loading animation
var allImages= @(new HtmlString(JsonConvert.SerializeObject(Model.FileAttachments.ToDictionary(k=>k.ID,v=>System.Convert.ToBase64String(v.FileData)))));
$.fancybox({
'scrolling' : 'no',
'titleShow' : true,
'title' : imageTitle,
'showCloseButton' : true,
'titlePosition' : 'outside',
//'titleFormat' : formatTitle,
//'transitionIn' : 'fade',
//'transitionOut' : 'fade',
//'speedIn' : 600,
//'speedOut' : 200,
'overlayShow' : false,
content: '<img style="height:500px; width:auto;" src="data:image/png;base64,'+ allImages[id] +'" />',
type: "html"
});
}
</script>
使用Newtonsoft.Json
@p、 文件名
函数showImage(id、imageTitle){
$.fancybox.showLoading();//显示加载动画
var allImages=@(新的HtmlString(JsonConvert.SerializeObject(Model.FileAttachments.ToDictionary)(k=>k.ID,v=>System.Convert.ToBase64String(v.FileData‘‘‘)’));
$.fancybox({
“滚动”:“否”,
“标题秀”:没错,
“标题”:imageTitle,
“showCloseButton”:true,
“标题位置”:“外部”,
//“标题格式”:格式标题,
//“transitionIn”:“fade”,
//“transitionOut”:“fade”,
//“speedIn”:600,
//“加速输出”:200,
“叠加显示”:错误,
内容:“”,
键入:“html”
});
}
我在这里猜测,但我假设您的值
id
未设置。错误消息是客户端(在浏览器开发控制台中)还是服务器端?什么是上下文-如何调用downloadFile()
以及何时以及如何调用您的c#code。。。第一个默认值(p=>p.ID==@ID).
生成了吗?更多的细节和代码(服务器端、生成的html、html页面)肯定会使帮助更容易。@three-fouronesixonethree我更新了我的问题。请确保id参数成功地传递给Javascript方法(我签入了Firebug)。有什么想法吗?你知道js不能直接从后面调用方法吗?如果你想调用,你需要ajax,第一个方法可以调用success,因为你输入的id是82,它是一个整数,你可以理解它NET@SkyFang事实上,我第一次使用Ajax,但我听说“这种操作不需要使用AJAX。图像是一个页面的单独资源,并且已经作为单独的请求加载。只需链接到它:“如第页所示。另一方面,除了在我的javascript方法中传递id之外,没有问题。那么,你认为呢?我必须使用Ajax吗?如果是这样的话,您能否发布一个适用于图像和excel/pdf文件的AJax方法,这些文件可以通过单击上面的链接打开?Thank@threeFourOneSixOneThree为什么投票反对而不是帮助?谢谢你的回复。编译器错误消息:CS0118:“Newtonsoft.Json.JsonConvert”是一个“类型”,但与“变量”一样使用。另一方面,如果你建议我使用Ajax而不是这个,你能通过更新我上面的Ajax方法来发布Ajax示例吗?我想将所有图像、png和其他文件类型(pdf等)列为超链接,单击此链接时,我想打开图像文件并下载其他文件类型。有什么帮助吗?投票+,但需要解决选择作为答案的问题。我以前丢失了SerializeObject
,fancybox无法下载其他文件,fancybox只是一个用于灯箱效果的Jquery插件,我已经回答了如何下载其他文件类型如果希望用户单击图像下载文件,您只需将a
添加到容器“image”中,如
非常感谢您的帮助。在您的帮助下,我解决了这个问题,并发布了最终代码,以便有人也可以使用它。投票+