J
J
JoraInTheSky2017-08-09 22:32:35
ASP.NET
JoraInTheSky, 2017-08-09 22:32:35

Ajax.BeginForm not properly generating Action?

There is a form for adding goods to the cart:

@using (Ajax.BeginForm("AddCart","Cart" , new AjaxOptions { UpdateTargetId = "res" }, new { style = "margin-top: 20px" }))
    {
        <div class="text-center">
            @Html.HiddenFor(r => r.Id)
            <input type="submit" class="btn btn-for-product" value="Добавить в корзину" />
        </div>
    }

Here is what is generated in the results:
<form action="/Products/AddCart?Length=4" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#res" id="form0" method="post" style="margin-top: 20px" novalidate="novalidate">        <div class="text-center">
            <input data-val="true" data-val-number="Значением поля Id должно быть число." data-val-required="Требуется поле Id." id="Id" name="Id" type="hidden" value="24">
            <input type="submit" class="btn btn-for-product" value="Добавить в корзину">
        </div>
</form>

The key here is that action="/Products/AddCart?Length=4" , but it should be action="/Cart/AddCart" . The helper contains a Cart controller and an AddCart action method. And the helper generates a strange action. I have a question, why does the helper form a strange action?? And how can this problem be solved from a helper?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JoraInTheSky, 2017-08-09
@JoraInTheSky

Need:

@using (Ajax.BeginForm("AddCart", "Cart", null, new AjaxOptions { UpdateTargetId = "res" }, new { style = "margin-top: 20px" }))
{
  ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question