发布带有操作链接的值(asp.net MVC JQuery)

发布于 2024-07-14 08:17:44 字数 159 浏览 7 评论 0原文

我的 MVC 视图页面上有一个。 当所选索引发生变化时,我使用 JQuery 来过滤网格。

在页面底部我有一个操作链接。 我想将此值发布到操作链接指向的控制器。

问题是,盒子的价值显然是可以改变的。

如何使用操作链接发布动态变化的数据?

I have an on my MVC View page. I use JQuery to filter a grid when the selected index changes.

At the bottom of the page I have an action link that. I would like to post this value to the controller that the action link is pointing to.

The problem is, obviously the value of the box can change.

How could I post dynamically changing data with an action link?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虚拟世界 2024-07-21 08:17:44

我想我会做的是使用表单而不是操作链接。 然后,您的 jQuery 将更新表单内与查询参数相对应的一些隐藏字段。 然后,您的“链接”(可能是一个按钮)将有一个提交表单的 onclick 处理程序。 如果您关心 url(我可以看到您可能关心的位置),只需使用正确的路由参数将控制器操作重定向到自身即可。

或者,您可以在选择更改时构造并替换 ActionLink 上的 href。 这将涉及在视图中维护代码以了解路由的构造方式。

注意:下面的示例可能会更好地使用围绕下拉列表本身的表单,并简单地在选择更改时提交,但该模式是我感兴趣的描述。

 <script type="text/javascript">
     $(document).ready( function() {
         $('#formLink').click( function() {
            $(this).parent('form').submit();
            return false;
         });
         $('#PageSizeMenu').change( function() {
             $('#PageSize').val( $(this).val() );
         })
     });
 </script>

 <%= Html.DropDownList("PageSizeMenu", ... ) %>
 <% using (Html.BeginForm( "List" )) { %>
    <a id="formLink" href="some-reasonable-link-for-non-javascript">Go</a>
    <%= Html.Hidden("PageSize") %>
 <% } %>

I think what I would do is use a form instead of an action link. Your jQuery would then update some hidden fields inside the form that correspond to the query parameters. Your "link" then (could be a button) would have an onclick handler that submits the form. If you care about the url -- and I can see where you might -- just have your controller action redirect to itself with the correct route parameters.

Alternatively, you could construct and replace the href on the ActionLink when the select changes. This would involve maintaining code in the view that understands how your routes are constructed.

Note: the following example would probably work better with the form wrapped around the dropdown itself and simply submitting on selection changed, but the pattern is what I'm interested in depicting.

 <script type="text/javascript">
     $(document).ready( function() {
         $('#formLink').click( function() {
            $(this).parent('form').submit();
            return false;
         });
         $('#PageSizeMenu').change( function() {
             $('#PageSize').val( $(this).val() );
         })
     });
 </script>

 <%= Html.DropDownList("PageSizeMenu", ... ) %>
 <% using (Html.BeginForm( "List" )) { %>
    <a id="formLink" href="some-reasonable-link-for-non-javascript">Go</a>
    <%= Html.Hidden("PageSize") %>
 <% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文