MVC Url.Content with javascript local variable

拥有回忆 提交于 2019-12-23 22:51:59

问题


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. My question is how do i point out that it should be a javascript variable and not a c# one ...?


回答1:


You cannot mix JavaScript and Razor in this way. Razor does not have any reference to it so it cannot use it to generate your link. 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.



来源:https://stackoverflow.com/questions/21485754/mvc-url-content-with-javascript-local-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!