以下代码不起作用。
我尝试了所有表格
@:
@()
@{}
<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: "860c13af-9aa9-4667-8ee8-c5629515c71b"
</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;
}
答案 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)
输出正确转义的字符串文字。