繁体   English   中英

如何在Telerik MVC网格模板列中建立链接将帖子发送到操作

[英]How to make a link in a Telerik MVC grid template column send a post to an action

这里有很多事情。

我有一个带有以下模板列的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.用正确的参数调用后处理方法。

谢谢你的帮助。

您的问题是,尽管您使用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;
});

暂无
暂无

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

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