且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法在Html帮助器中使用javaScript语法

更新时间:2023-09-07 14:11:22

答案是:没有办法!

因为:

你无法通过服务器将任何东西从javascript传递到剃刀。

You cannot pass anything from javascript to razor without going through the server.

这是因为剃刀是在服务器端执行的(因此它中的c#)。调用控制器,该类被实例化,然后调用与所请求的操作匹配的方法,然后返回视图。返回视图时,将执行视图中的c#代码。一旦所有代码都被执行,它会在响应中发出html页面,然后javascript在客户端上运行。为了让你从javascript到razor,你必须发出一个新的请求,从头开始经过这条路径,以便最终回来。

That is because the razor is executed server side (hence the c# in it). The controller is called, that class is instantiated, then the method is invoked which matches the action requested, and then a view is returned. When the view is returned, the c# code in the view is executed. Once all code has been executed, it issues the html page in the response and then the javascript runs on the client. For you to get from javascript to razor, you would have to issue a new request to go through that path from the beginning in order to come back out in the end.

解决此问题服务器端。

更多详细信息,请参阅此链接:

More detailed information please see this link:

将js变量或inout值传递给剃刀