有没有办法在Html帮助器中使用javaScript语法 [英] Is there a way to use javaScript syntax inside Html helper

查看:122
本文介绍了有没有办法在Html帮助器中使用javaScript语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的剃刀视图中有以下代码: -

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

但我无法使用以下 $(#pagesize)。val()获取字段的值,如果我可以在Html帮助器中使用类似javaScript的语法,那么任何人都可以使用吗?
谢谢

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 ? Thanks

推荐答案

答案是:没有办法!

因为:

你无法通过服务器将任何东西从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值传递给剃刀

这篇关于有没有办法在Html帮助器中使用javaScript语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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