MVC Url.Content使用JavaScript局部变量 [英] MVC Url.Content with javascript local variable

查看:662
本文介绍了MVC Url.Content使用JavaScript局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的问题:

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

号是本地JavaScript变量,但它给了我一个错误说,是不是在上下文中。我的问题是如何让我指出,这应该是一个javascript变量,而不是一个C#1 ...?

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 ...?

推荐答案

您不能以这种方式混合使用JavaScript和剃刀。剃刀没有任何提及它,因此它不能用它来生成你的链接。试试这个:

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);

您可能必须使用Url.Action如果你从服务控制器的图像而不是静态库。

You might have to use "Url.Action" if you're serving the images from a controller rather than a static repository.

这篇关于MVC Url.Content使用JavaScript局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆