简体   繁体   English

带有javascript局部变量的MVC Url.Content

[英]MVC Url.Content with javascript local variable

I have problem with this: 我对此有疑问:

var id=5;
var el = $("MainPhotoHolder");
el.attr("src", '@Url.Content("~/Page/GetImage/" + id)');

id is a local javascript variable, but it gives me an error saying that is not in the context. id是一个本地javascript变量,但是它给我一个错误,指出它不在上下文中。 My question is how do i point out that it should be a javascript variable and not ac# one ...? 我的问题是我如何指出它应该是一个JavaScript变量而不是ac#one ...?

You cannot mix JavaScript and Razor in this way. 您不能以这种方式混合使用JavaScript和Razor。 Razor does not have any reference to it so it cannot use it to generate your link. Razor没有任何引用,因此无法使用它生成您的链接。 Try this: 尝试这个:

el.attr("src", '@Url.Content("~/Page/GetImage/")' + id);

You might have to use "Url.Action" if you're serving the images from a controller rather than a static repository. 如果要从控制器而不是静态存储库提供图像,则可能必须使用“ Url.Action”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM