I am generating the view layout based on the screen resolution using javascript, all DIV and tables work well except the Html.ActionLink. Converting:

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

using:

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("}))");

Did not work, I get this error:

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

Tried to add ")" at the end but did not work; would appreciate your suggestions.

有帮助吗?

解决方案

Razor code is server side and is sent to the client already rendered. Getting the client to write Razor code in javascript will not work.

Instead, you can just write your <a></a> tag directly. You can use some server side variables though using razor. For example:

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

That way you can pick up your server side values and create the link as you require it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top