简体   繁体   English

是否可以在HTML帮助器中使用javaScript语法

[英]Is there a way to use javaScript syntax inside Html helper

I have the following code inside my razor view:- 我的剃刀视图中包含以下代码:-

@Html.PagedListPager(Model , page => Url.Action("Index","Server", new 
{
    searchTerm = ViewBag.searchTerm,
    page,
    sort = ViewBag.CurrentSortOrder,
    pagesize =  $("#pagesize").val()  
}),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, 
new AjaxOptions 
{
    UpdateTargetId = "ServerTable" , 
    LoadingElementId="progress2" 
}))

But i am unable to get the value of field using the following $("#pagesize").val() , so can anyone adivce if i can use a javaScript like syntax inside an Html helper ? 但是我无法使用下面的$("#pagesize").val()来获取field的值,所以如果我可以在HTML帮助器中使用像javaScript这样的语法,那么谁能离开呢? Thanks 谢谢

The answer is : NO WAY! 答案是:不行!

Because: 因为:

You cannot pass anything from javascript to razor without going through the server. 如果不通过服务器,就无法将任何内容从javascript传递到剃刀。

That is because the razor is executed server side (hence the c# in it). 那是因为剃须刀是在服务器端执行的(因此在其中执行了c#)。 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. 返回视图后,将执行视图中的c#代码。 Once all code has been executed, it issues the html page in the response and then the javascript runs on the client. 一旦执行完所有代码,它将在响应中发出html页面,然后javascript在客户端上运行。 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. 为了使您从javascript到剃须刀,您必须发出一个新请求以从头开始遍历该路径,以便最后返回。

Solve this issue server side. 解决此问题的服务器端。

More detailed information please see this link: 更多详细信息,请参见以下链接:

Pass js variable or inout value to razor 将js变量或inout值传递给剃刀

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM