user1624220
user1624220

Reputation: 25

Html.BeginForm does not call action method in MVC

I have a partial view:

@model MsmStore.Domain.Products

<h3 class="lead">
    @Model.Name
</h3>
@*        <img src="~/images/MainLogo.jpg"/>*@
<p>
    @Model.Decription
</p>
<p>
    @Model.Price
</p>

@using (Html.BeginForm("UpdateBasketSummary", "Order", new { ProductID = Model.ID }))
{
    @Html.Hidden("ProductID", Model.ID)
    <input type="button" value="Click" />
}

this Html.BeginForm does not call action method (UpdateBasketSummary).

and this is my Action Method:

    [HttpPost]
    public PartialViewResult UpdateBasketSummary(string ProductID)
    {
//          orderRepository.AddToCart(ProductID, 1);
            return PartialView("BasketSummary", ProductID);
    }

this is my routing code:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(null, "{controller}/{action}");

Upvotes: 1

Views: 4395

Answers (4)

Brent M Clark
Brent M Clark

Reputation: 112

Your action requires the http method POST and yet your beginForm call does not set the http method to POST. Without explicitly setting the http method you will be forced to use the default, which is a GET.

Upvotes: 0

Ahmad Harb
Ahmad Harb

Reputation: 615

Input of type button will not post your form, replace it with..

<input type="submit" value="Click" />

Upvotes: 1

Sergey Boiko
Sergey Boiko

Reputation: 471

please, try this:

@using (Html.BeginForm("UpdateBasketSummary", "Order"))
{
    @Html.Hidden("ProductID", Model.ID)
   <input type="button" value="Click" />
}

[HttpPost]
public PartialViewResult UpdateBasketSummary(int ProductID)
{
   //orderRepository.AddToCart(ProductID, 1);
   return PartialView("BasketSummary", ProductID);
}

btw. not need to send ProductID in BeginForm, as you already did it by Hidden field

Edited: due to @Ben Griffiths answer: you also need to change type='button' to type='submit' of course

Upvotes: 0

Ben Griffiths
Ben Griffiths

Reputation: 1696

An input with type button will not submit the form, by default. You need to change it to

 <input type="submit" value="click" />

or

<button type="submit" value="click" />

Upvotes: 3

Related Questions