Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript字符串中的Razor tilde_Javascript_Asp.net_Razor - Fatal编程技术网

JavaScript字符串中的Razor tilde

JavaScript字符串中的Razor tilde,javascript,asp.net,razor,Javascript,Asp.net,Razor,在JavaScript字符串中使用Razor中可用的tilde“~”字符设置应用程序根时遇到问题。我正在使用JavaScript为不同的情况更改链接的href属性。使用Razor在构建时插入应用程序根目录的正确方法是什么 我能得到的最接近的无错误代码是使用以下代码: $('#ControlsSave').attr('href', “@(“~/@Page.Section/@Page.Subsection/”)DVRDetails.cshtml?评级=”+ 额定值[额定值]) 它在生成时生成此URL

在JavaScript字符串中使用Razor中可用的tilde“~”字符设置应用程序根时遇到问题。我正在使用JavaScript为不同的情况更改链接的href属性。使用Razor在构建时插入应用程序根目录的正确方法是什么

我能得到的最接近的无错误代码是使用以下代码:

$('#ControlsSave').attr('href', “@(“~/@Page.Section/@Page.Subsection/”)DVRDetails.cshtml?评级=”+ 额定值[额定值])

它在生成时生成此URL:

href=“~/@Page.Section/@Page.Subsection/DVRDetails.cshtml?rating=TV-Y”

但它必须是:


href=“localhost:1234/MyCustomSection/MyCustomSubsection/DVRDetails.cshtml?rating=TV-Y”
尝试使用
Url.Content

    @Url.Content(
    string.Format(
        "~/{0}/{1}/DVRDetails.cshtml",
        Page.Section,
        Page.Subsection))
完整用法应如下所示:

$('#ControlsSave').attr('href', '@Url.Content(string.Format("~/{0}/{1}/DVRDetails.cshtml", Page.Section, Page.Subsection))' + '?rating=' + rating[ratingNum])

括号内的数字有什么作用?这不起作用。也许我把语法搞错了。我需要一个例子,说明如何像我的问题一样将其插入JavaScript<代码>$('#ControlsSave').attr('href',我在这里放什么?)@Ben,这些是
string.Format
用于按外观顺序插入参数的占位符。例如,此处的
{0}
将替换为
页面。小节
{1}
替换为
页面。小节
{2}
替换为
评级[ratingNum]
。您不需要这样做,并且可以继续使用普通连接,但是
string.Format
通常更可读。它不起作用。可能是因为ratingNum部分是一个JavaScript数组。@Ben,哦,那确实是另一回事,我不知道。我会更新答案