asp.net MVC 5 中的同一控制器上带有 httpPost 的 ActionLink

ActionLink with httpPost on same controller in asp.net MVC 5

我有一个登录表单 "forgot password" link。 在登录提交时,我调用了一些其他操作,而在 "forgot password" link 我调用了一些不同的操作:

 @Html.ActionLink("Forgot Password", "ForgotPassword", "Login",  Model , new { @onclick = "return ValidateUsername();" })

我需要的是将其作为 POST 而不是 GET 发送。

我拥有的是 ASP.NET WebForm 解决方案,我几乎完全将其转换为 ASP.NET MVC。我在一个地方使用 LinkBut​​ton。这是唯一的瓶颈。


编辑:

现在我正在使用以下JavaScript

$("a.anchor").click(function (e) {
    e.preventDefault(); // This will stop default behavior
    var validate = ValidateUsername();
    if (validate) {
        //alert(document.getElementById('usernameTextBox').value);

        var str = document.getElementById('usernameTextBox').value;
        $.ajax({
            type: "POST",
            url: "http://localhost/SSOMVC/Generic/ForgotPassword",
            data: "data",
            dataType: "text",
            success: function (resultData) {
                document.getElementById("usererror").textContent = resultData;
            },
            failure: function (error) {
            }
        });
    }
});

 @Html.ActionLink("Forgot Password", null, null, Model, new { @class = "anchor" })


  public class GenericController : Controller
    {


        [HttpPost]
        public ActionResult ForgotPassword(string data)
        {
           ....
            return Content(callResponse);
        }
    }

不知道为什么我总是在控制器中获取数据为空。

ActionLink在MVC中被转换为锚标签。锚标记默认发送 GET 请求。

要发出 Post 请求,您可以使用 Ajax post 调用 jquery.

   @Html.ActionLink("Forgot Password", "ForgotPassword", "Login",  Model , new { @onclick = "return ValidateUsername();" }, new { @class="anchor"})

    <script type="text/javascript">

    $("a.anchor").click(function(e){
      e.preventDefault(); // This will stop default behavior
     $.ajax({
      type: "POST",
      url: "<your classname/methodname>",
      data: <your data>,
      dataType: "text",
      success: function(resultData){
          alert("Call Complete");
      },
      failure: funciton(error){
}
});
    });    

</script>