-2

I have a login form with "forgot password" link. On login submit I am calling some other action while on "forgot password" link I am calling some different action:

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

What I need is to send it as POST instead of GET.

What I have was a ASP.NET WebForm solution which I have almost completely converted into ASP.NET MVC. One place I was using LinkButton. That is the only bottle neck remains.


Edit:

Now I am using following 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);
        }
    }

Don't know why I am always getting data as null in the controller.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • I don't think you can even do that with actionlink; if you can it'll be in one of the overloads – The Bearded Llama Sep 08 '17 at 10:39
  • you can read this topic : https://stackoverflow.com/questions/2048778/asp-net-mvc-actionlink-and-post-method – arslanaybars Sep 08 '17 at 10:40
  • The question is Not clear 'Model ' you can pass in Model in directly in ActionLink – umasankar Sep 08 '17 at 10:41
  • Model was not passing in the action that's why i have passed it – Kamran Shahid Sep 08 '17 at 10:43
  • 2
    You need a `
    ` to make a POST
    –  Sep 08 '17 at 11:07
  • What form Stephen. in my Ravor view there is already a for with @using (Html.BeginForm("LoginWithPassword", "Login", FormMethod.Post)) {.... – Kamran Shahid Sep 08 '17 at 12:52
  • Then You should make separate form and convert this link into submit button. Then add hidden input with username and copy the value from previous form. But i would advise to make such form on separte site. – RedgoodBreaker Sep 09 '17 at 20:23
  • It's not really clear what you're trying to do, but at a minimum, you need to change your data option to `data: { data : str }`; $.ajax will take care of serializing that correctly. Also, if you're using jQuery, then be consistent: `document.getElementById('usernameTextBox').value` would be `$('#usernameTextBox').val()`. – Tieson T. Sep 11 '17 at 08:18

1 Answers1

2

ActionLink is converted into anchor tag in MVC. An anchor tag by default sends a GET Request.

To make a Post request, you can use Ajax post call in 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>
Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80