【问题标题】:Webgrid select row without using SelectLinkWebgrid 选择行而不使用 SelectLink
【发布时间】:2013-08-25 01:25:04
【问题描述】:

我正在使用 MVC4 和 Razor。

在我的 cshtml 文件中,我有一个 Webgrid。如何在不使用 selectLink 或复选框或单选按钮的情况下选择一行?

我想在这里实现的场景是 -

在 Webgrid 中显示包含数据的列,然后选择一行而不使用 selectLink 或类似的东西。并基于此选择,将此值发送给 Controller。

至于我到目前为止所尝试的... 我一直在从下面的这些链接中关注教程 - Example 1 Example 2

这两个例子都很好,但它们都使用了 SelectLink 方法。谁能举个例子?

【问题讨论】:

  • 那么您希望如何选择该行?只需单击行本身,还是?

标签: c# asp.net-mvc-4 razor


【解决方案1】:

除了使用 WebGrid 自己的功能之外,您还可以使用一些 jQuery 在他们单击行中的任意位置时触发行选择,如下所示:

$(function () {

    // this selector should target the WebGrid table, with an id or class that is
    // set on the table
    $('table > body > tr').click(function () {

        // this will depend on how you action and routes are set up but this will
        // work for the default route of "{controller}/{action}/{id}"
        // also this will simply redirect to the URL with the selected row value,
        // if your intention is to stay on the page consider using jQuery's ajax
        // methods (i.e. .load(), $.get(), $.post() or $.ajax())
        $(location).attr('href', @Url.Action("YourAction", "YourController") + '/' + /* get row value */)

    });

});

【讨论】:

  • 您好,谢谢您的回复。我对 JQuery 有点陌生。你能解释一下这部分的含义和作用吗? $('table > body > tr') 我知道一旦用户在行内单击,就会调用 click 函数。我不明白这个('table > body > tr')......再次感谢。我真的很感激。
  • 那块叫做 jQuery 选择器api.jquery.com/category/selectors 他们基本上遵循 CSS 选择器的规则。语法 $(table > tbody > tr) 从所选元素创建一个 jQuery 对象,在这种情况下,所有 tr 元素都是 body 元素的子元素,而这些元素又是 table 元素的子元素。
猜你喜欢
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 2012-04-30
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多