简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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