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: "860c13af-9aa9-4667-8ee8-c5629515c71b"
</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;
}
Model.IDa string?Model.IDand 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 ofProjectID, it is dynamic. Should get theModel.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.