3

The code below does not work.

I've tried all forms

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

The apparent problem is the { and } at the beginning and end. (Required, as are parameters)

<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>

How to use, @Model.ID in my JavaScript?

[Edit] Adding info

With this code:

<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>

The razor generate this code:

<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>

[Add] Another example of code

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

If you set the value manually, everything works perfectly!

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

[Edit] Resolution

To correct the problem, I had to do this: (not an elegant way)

@{
    @: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;
}
7
  • 2
    Define "does not work". What is it supposed to do? What does it do? What error messages do you get (if any)? Commented Nov 14, 2011 at 2:34
  • That should work fine. What do you see in the rendered source? Is Model.ID a string? Commented Nov 14, 2011 at 2:34
  • Model.ID and the object ID. When generate the HTML should look something like: SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: "123456"} ); Note the value of ProjectID, it is dynamic. Should get the Model.ID. For me not works! The HTML generated is wrong, see the Javascript code that generates this code. It does not generate the complete HTML, just to 'ProjectID: 123456' And there!. The rest of the JavaScript is not generated. Commented Nov 14, 2011 at 4:06
  • You have a sytnax issue in the surrounding code. Please show a complete file that demonstrates this problem. Commented Nov 16, 2011 at 23:53
  • Put all the details (including the complete code) this link: gist.github.com/1371965 Commented Nov 17, 2011 at 0:15

3 Answers 3

1
window.onload = initializeFileUploader();

This is executing initializeFileUploader() straight away, BEFORE window.onload is actually called, it should be

window.onload = initializeFileUploader;
Sign up to request clarification or add additional context in comments.

1 Comment

See my changes in the question please.
1

Try putting @Model.ID in single quotes '@Model.ID'. This always works for me.

Here is the code:

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;

5 Comments

I do not know why, but it does not work for me! This was what the 'razor' generated: i.imgur.com/CYx9W.png This is the end of the HTML code: <script type="text/javascript"> function initializeFileUploader() { SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: 'ad9bc1d0-ab0b-45a9-8fe8-f7fb79146abb', true) </body> </html>
I see that the ID is interpreted as it should. Have you tried hardcoding a value to see if it will render successfully? It would also be helpful if you can provide us with the whole markup of the view.
Hello, I modified my function. I created a jQuery plugin to encapsulate everything. Was as follows: $('#Logo').bindAjaxUploader({ action: '/Project/ImageUpload', allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], params: { Logo: true, ProjectID: "559b0505-09f2-4385-aee0-530d79c82803" } }); As you can see I put the ID value in the parameter ProjectID
$('#Logo').bindAjaxUploader({ action: '/Project/ImageUpload', allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], params: { Logo: true, ProjectID: @HttpUtility.JavaScriptStringEncode(Model.ID.ToString(), true) } }); The same function, but with the dynamic value, does not work! Razor just does not generate the HTML code from ProjectID:
I noticed I had an error in my code above. Please try again. I have edited my answer.
0

Both of your code samples should work fine.

My psychic powers tell me that Model.ID is a string, so you end up writing the string value to the Javascript without quotes. Therefore, it gets parsed as a variable name rather than a value.
Instead, call @HttpUtility.JavaScriptStringEncode(Model.ID, true) to output a properly-escaped string literal.

9 Comments

user551841's answer is correct; I missed that. This might be a separate problem.
See my changes in the question please.
Sorry; I forgot to add that you need Html.Raw to prevent it from HTML-escaping the Javascript. You may want to wrap all that in your own extension method.
It looks like you have a syntax error in your surrounding HTML that is causing Razor to think it's in code context. Look for unmatched tags and stray @s.
Would you post an example? I tried as follows: function initializeFileUploader() { SetUploader('/Projects/ImageUpload', 'Images', true, ['jpg', 'jpeg', 'png', 'gif', 'zip'], { ProjectID: @Html.Raw(HttpUtility.JavaScriptStringEncode(Model.ID.ToString(), true)) }); SetUploader('/Projects/ImageUpload', 'Logo', false, ['jpg', 'jpeg', 'png', 'gif'], { Logo: true, ProjectID: @Html.Raw(HttpUtility.JavaScriptStringEncode(Model.ID.ToString(), true)) } ); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.