document.write @(Html.ActionLink

时间:2014-05-15 22:03:58

标签: javascript asp.net-mvc-4 razor

我使用javascript根据屏幕分辨率生成视图布局,除了Html.ActionLink之外,所有DIV和表都运行良好。转换:

@(Html.ActionLink("Back", "index", "home", new{loggedIn = ViewBag.id}, new {@class = "t-button", @style = "width: 200px; color: white; text-align: center" }))

使用:

document.write("@(Html.ActionLink(\"Back\", \"index\", \"home\", new");
document.write("{");
document.write("loggedIn = ViewBag.id");
document.write("}, new");
document.write("{");
document.write("@class = \"t-button\",");
document.write("@style = \"width: 200px; color: white; text-align: center\"");
document.write("}))");

没有用,我收到了这个错误:

The explicit expression block is missing a closing ")" character.

试图在最后添加“)”但没有奏效;非常感谢你的建议。

1 个答案:

答案 0 :(得分:0)

Razor代码是服务器端,并发送到已经呈现的客户端。让客户端在javascript中编写Razor代码是行不通的。

相反,您可以直接编写<a></a>标记。您可以使用razor使用某些服务器端变量。例如:

document.write('<a class="t-button" style="width: 200px; color: white; text-align: center" href="index/home?loggedin=@ViewBag.id" >Back</a>');

这样您就可以获取服务器端值并根据需要创建链接。

相关问题