名称“ID”不Javascript方法目前情况下存在 [英] The name 'id' does not exist in the current context in Javascript method

查看:127
本文介绍了名称“ID”不Javascript方法目前情况下存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能如下图所示尝试多种途径使用传递给我的JavaScript方法的Lambda前pression参数。如何使用id参数下前pression?先谢谢了。

有是文件名的超链接和我通过ID参数给Javascript方法成功:

 <一的onclick =downloadFile(@ p.ID);> @ p.FileName< / A>功能downloadFile(ID){
    $ .fancybox({
        //这工作:(p.ID == 82)
        内容:'< IMG SRC =数据:图像/ PNG; BASE64,@ System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(P => p.ID == 82).FileData)ALT =/ >',        //他们不是工作:(p.ID == ID / p.ID == @id / p.ID == this.id)
        内容:'< IMG SRC =数据:图像/ PNG; BASE64,@ System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(P => p.ID == ID).FileData)ALT =/ >',        内容:'< IMG SRC =数据:图像/ PNG; BASE64,@ System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(P => p.ID == @id).FileData)ALT = />',        内容:'< IMG SRC =数据:图像/ PNG; BASE64,@ System.Convert.ToBase64String(Model.FileAttachments.FirstOrDefault(P => p.ID == this.id).FileData)ALT = />,        类型:HTML
    });
}


更新:这里是我以前使用的方法阿贾克斯

 函数downloadFile(ID){
    $阿贾克斯({
        URL:/问题/ RenderImage
        键入:POST,
        数据:JSON.stringify({ID:ID})
        数据类型:JSON
        传统:真实,
        的contentType:应用/ JSON的;字符集= UTF-8,        成功:函数(响应){
            $ .fancybox({
                内容:'&下; IMG高度=200的宽度=250SRC =数据:图像/ PNG;的base64,'+ response.Image +/>',
                类型:HTML
            //});
        },
        错误:函数(){
            警报(发生了错误!!!);
        }
    });
}


解决方案

如果有人告诉你,有没有必要AJAX,它似乎可以将图像数据后页,就像字典,你可以改变你的code ,并把你的 Model.FileAttachments 是JS的一部分,就像

  VAR allPictures = @JsonConvert.SerializeObject(Model.FileAttachments.ToDictionary(k=>k.ID,v=>System.Convert.ToBase64String(v.FileData)));
功能downloadFile(ID){
    $ .fancybox({
        内容:'< IMG SRC =数据:图像/ PNG; BASE64,'+ allPictures [ID] +'ALT =/>,
        类型:HTML
    });
}

和你说你要下载的其他类型的文件(PDF等),它不能像图像,你可以用code这样的,如果没有限制要下载的文件

 < A HREF =文件路径>文件名< / A>

I cannot use the parameter passed to my javascript method in lambda expression by trying several ways as shown below. How can I use id parameter in the expression below? Thanks in advance.

There is a hyperlink in the FileName and I pass the ID parameter to the Javascript method successfully:

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


Update: Here is the Ajax method that I used before

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

解决方案

If somebody told you there is no need ajax, it seems the image data can be post to page,just like dictionary,you can change your code ,and put your Model.FileAttachments to be part of js,just like

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

And you said you want to download other file types (pdf, etc),it can not be like image,you can use code like these if there is no limited to download the file

<a href="filePath">fileName</a> 

这篇关于名称“ID”不Javascript方法目前情况下存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆