【问题标题】:How to make a link in a Telerik MVC grid template column send a post to an action如何在 Telerik MVC 网格模板列中创建链接将帖子发送到操作
【发布时间】:2012-04-18 20:39:40
【问题描述】:

这里发生了很多事情。

我有一个带有以下模板列的 Telerik MVC 网格:

@(Html.Telerik().Grid((IEnumerable<User)Model.Data)
  .Name("UsersGrid")
  .DataKeys(keys => keys.Add(c => c.UserName))
  .DataBinding(dataBinding => dataBinding.Server()
  .Columns(c =>
  {
    c.Bound(r => r.FullName).Title("");
    c.Bound(r => r.UserName).Title("");
    c.Template(
      @<text>
        @if(@item.Status == "Pending")
        {
           @Html.ActionLink("Resend Invite", "ResendInvite", new { Email = @item.UserName, FirstName = @item.FullName }, new { @class = "reesendInviteLink" })
        }
      </text>
    ).Title("Link").HtmlAttributes(new { Style = "text-align: right;" });
  }));

现在我知道 ActionLink 不会调用发布操作,所以我正在使用 jquery 执行以下操作:

$(document).ready(function () {
   $(".resendInviteLink").click(function (e) {
      var url = e.currentTarget.href;
      $.post(url);
   });
});

我尝试调用的 Action 方法如下所示:

[HttpPost]
public ActionResult ResendInvite(UserVM user)
{
   //....Do Something
}

当我调试 jquery 时,一切都很顺利,直到我到达 $.post,然后它失败了,说它在控制器上找不到 ResendInvite 操作。在某种程度上我认为这是有道理的,因为 ActionLink 正在寻找 Get,而不是 Post。

那么如何在网格上创建一个链接:
1. 从 Telerik 网格中获取电子邮件和用户名。
2. 调用具有正确参数的后操作方法。

感谢您的帮助。

【问题讨论】:

    标签: c# jquery asp.net-mvc-3 telerik-mvc


    【解决方案1】:

    您的问题是,虽然您已经使用 Jquery 订阅了链接的 click event,但链接的原始 click event 仍然会触发一个失败的 Get 请求。

    你需要使用preventDefault方法

    $(".resendInviteLink").click(function(e) {
        e.preventDefault();
        var url = e.currentTarget.href;
        $.post(url);
    });
    

    return false 形成事件处理程序:

    $(".resendInviteLink").click(function(e) {
        var url = e.currentTarget.href;
        $.post(url);
        return false;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多