3

I am using WebGrid in My MVC application , for paging I am using this code

  var links = $('a[href*=page], a[href*=sort]');
        form = $('form')[0];
        links.click(function () {
            debugger;
            form.attr("action", this.href);
            $(this).attr("href", "javascript:");
            form.submit();
        });

@using (Html.BeginForm(Model.PostToAction, "Leads", FormMethod.Post))

when form.submit() is executed it is Executed as GET and not POST ,

what am I doing wrong ?

UPDATE:

My form is :

<form action="/Leads/DetailsLeads" method="post">

Controller is :

 [HttpPost]
        public ActionResult DetailsLeads(LeadDetailsViewModel model)
StringBuilder
  • 1,619
  • 4
  • 32
  • 52
  • could you try this: `form.attr("method","post");` – AliRıza Adıyahşi Nov 29 '12 at 15:03
  • Have you read the rendered HTML to verify that it is being created as a "post" form method? Could you paste the controller declaration(s)? Also, just so I am clear, are you changing the action URL to the respective clicked link's href? – DeeDub Nov 29 '12 at 15:17
  • 3
    Try adding e.preventDefault() inside of your links.click(function(){} block. The browser may still be trying to follow the link you clicked (GET request) – Tommy Nov 29 '12 at 15:20
  • @Tommy: You should submit that as a viable answer. – Joel Etherton Nov 29 '12 at 15:29
  • @JoelEtherton - thanks, I did. I was in a hurry when I first came across this (was actually looking for an answer to something I was working on)! – Tommy Nov 29 '12 at 15:32
  • Tommy , Thanks it Helped ! Convert your cooment into answer please , I think it is very usefull ! – StringBuilder Nov 29 '12 at 15:37
  • The problem was in some code before submit , and since there were no e.PreventDefaults error couldn't be seen !!! Thanks – StringBuilder Nov 29 '12 at 15:39
  • @StringBuilder - glad to help! comment has been posted as an answer :) – Tommy Nov 29 '12 at 15:42

1 Answers1

7

Try adding e.preventDefault() inside of your links.click(function(){} block. Without this, the browser will still attempt to do the action you instructed it to do (a GET request on the clicked link).

var links = $('a[href*=page], a[href*=sort]');
        form = $('form')[0];
        links.click(function (e) {
            e.preventDefault();
            debugger;
            form.attr("action", this.href);
            $(this).attr("href", "javascript:");
            form.submit();
        });
Tommy
  • 39,592
  • 10
  • 90
  • 121