John
John

Reputation: 3546

MVC form post not reaching controller

I have a form that on submit doesn't reach the controller method (I put a breakpoint there)

    [HttpPost]
    public ActionResult SaveBilling( FormCollection fc )
    {
...
}

the frontend code is something like this

<form action="/PersonSettings/SaveBilling" method="post" >
...
<input type='submit' value="save" />
</form>

any ideas ?

not sure if it is a route handler problem because it does reach the GET version if i go to /PersonSettings/SaveBilling in browser but the post method just yields a blank page and doesnt go into the code

Upvotes: 2

Views: 5578

Answers (5)

Eli
Eli

Reputation: 444

Sometimes using a network sniffing tool like Fiddler is helpful to see what's wrong when the request is sent and it seems not reaching the server.

Upvotes: 0

Jon S
Jon S

Reputation: 168

I realize this has been solved, but I thought I'd tack on another possible cause: Having the [ValidateAntiForgeryToken] attribute on your method without an @Html.AntiForgeryToken() in your code will create the same problem.

Upvotes: 1

Mohammad Atiour Islam
Mohammad Atiour Islam

Reputation: 5708

use form and define action path.

<form id="subscriptionForm" action="/Category/Create" method="post"> 

--your div 

</form >

Now use script to serialize the form

<script type="text/javascript">
        $('#Save').click(function () {
            var form = $("#subscriptionForm");
            var url = form.attr("action");
            var formData = form.serialize();
            $.post(url, formData, function (data) {
                $("#msg").html(data);
            });
        })
    </script>

Now you can reach your controller action.

Upvotes: 0

John
John

Reputation: 3546

Turns out it was a routing related issue after all , I have some complex routes and the default mvc route was catching things that should have gone through the default mvc route with areas.

Was able to recreate the issue by having explicit parameters for my function instead of a formcollection, so that might have been the issue ohwell.

Upvotes: 0

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

Rewrite your view as:

@using (Html.BeginForm("SaveBilling", "PersonSettings", FormMethod.Post))
{
    ....
    <input type='submit' value="save" />
}

Is this controller in any area or not?

Upvotes: 5

Related Questions