Javascript代码中的代码剃刀

时间:2011-11-14 02:25:50

标签: javascript asp.net-mvc-3 razor

以下代码不起作用。

我尝试了所有表格

@: @() @{} <text> </text>

明显的问题是开头和结尾的{}。 (必需,参数也是如此)

<script type="text/javascript">

    function initializeFileUploader()
    {
        SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: @Model.ID } );
        SetUploader('/Projects/ImageUpload', 'Logo', false, ['jpg', 'jpeg', 'png', 'gif'], { Logo: true, ProjectID: "@(Model.ID)" } );
    }
    window.onload = initializeFileUploader;

</script>

如何在我的JavaScript中使用@Model.ID

[编辑]添加信息

使用此代码:

<script type="text/javascript">

    function initializeFileUploader()
    {
        SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: @HttpUtility.JavaScriptStringEncode(Model.ID.ToString(), true) });
        SetUploader('/Projects/ImageUpload', 'Logo', false, ['jpg', 'jpeg', 'png', 'gif'], { Logo: true, ProjectID: @HttpUtility.JavaScriptStringEncode(Model.ID.ToString(), true) } );
    }

    window.onload = initializeFileUploader();

</script>

剃刀生成此代码:

<script type="text/javascript">

        function initializeFileUploader()
        {
            SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: &quot;860c13af-9aa9-4667-8ee8-c5629515c71b&quot; 

</body>
</html>

[添加] 代码的另一个例子

$('#Logo').bindAjaxUploader({
    action: '/Project/ImageUpload',
    allowedExtensions:  ['jpg', 'jpeg', 'png', 'gif'],
    params: { Logo: true, ProjectID: @HttpUtility.JavaScriptStringEncode(Model.ID.ToString(), true) }
});

如果您手动设置值,一切正常!

$('#Logo').bindAjaxUploader({
    action: '/Project/ImageUpload',
    allowedExtensions:  ['jpg', 'jpeg', 'png', 'gif'],
    params: { Logo: true, ProjectID: "559b0505-09f2-4385-aee0-530d79c82803" }
});

[编辑]分辨率

要解决问题,我必须这样做:(不是优雅的方式)

@{
    @:function initializeFileUploader()
    @:{
            @:SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: "@(HttpUtility.JavaScriptStringEncode(Model.ID.ToString()))" });
            @:SetUploader('/Projects/ImageUpload', 'Logo', false, ['jpg', 'jpeg', 'png', 'gif'], { Logo: true, ProjectID: "@(HttpUtility.JavaScriptStringEncode(Model.ID.ToString()))" });
        @:}

    @:window.onload = initializeFileUploader;
}

3 个答案:

答案 0 :(得分:1)

window.onload = initializeFileUploader();

这是直接执行initializeFileUploader(),实际上是在调用window.onload之前,它应该是

window.onload = initializeFileUploader;

答案 1 :(得分:1)

尝试将@ Model.ID放在单引号'@ Model.ID'中。这总是适合我。

以下是代码:

function initializeFileUploader()
{
    SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: '@Model.ID' });
    SetUploader('/Projects/ImageUpload', 'Logo', false, ['jpg', 'jpeg', 'png', 'gif'], { Logo: true, ProjectID: '@Model.ID' });
}

window.onload = initializeFileUploader;

答案 2 :(得分:0)

您的两个代码示例都可以正常工作。

我的通灵能力告诉我Model.ID是一个字符串,所以你最终将字符串值写入Javascript而没有引号。因此,它被解析为变量名而不是值 相反,请调用@HttpUtility.JavaScriptStringEncode(Model.ID, true)输出正确转义的字符串文字。