在剃刀ActionLink的JavaScript变量 [英] Javascript variable in razor ActionLink

查看:78
本文介绍了在剃刀ActionLink的JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var boxIdValue = 233;
var result = title + '<br/>@Html.ActionLink("Detail", "Show", "Boxes", new{boxId=233}, null)';

当我辛苦code boxId 然后它工作。但是,当我写:

When I hardcode boxId then it works. But when I write:

var result = title + '<br/>@Html.ActionLink("Detail", "Show", "Boxes", new{boxId=boxIdValue}, null)';

它没有。是否有可能以这种方式的JavaScript var和剃须刀混?

It doesn't. Is it possible to mix javascript var and razor in this way?

推荐答案

在看一看<一个href=\"http://stackoverflow.com/questions/9745139/building-relative-urls-for-an-mvc-app-with-javascript/\">this相关堆栈溢出问题。

之所以这是一个挑战是,剃刀方法执行Web服务器在渲染时间,而JavaScript的运行时客户端浏览器上执行。

The reason why this is a challenge is that the Razor method executes on the web server at render time while the javascript executes on the client browser at runtime.

我会做类似

var boxIdValue = 233;
var link = '@Html.ActionLink("Detail", "Show", "Boxes", new{boxId=-1}, null)'
link = link.replace('-1', boxIdValue);
var result = title + '<br />' + link;

这篇关于在剃刀ActionLink的JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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