0

The issue is that the form submission event is triggered but the relevant action isn't being called. Do I have to mention the action name in the submit event of the form or will it figure it automatically?

Below you can see the code.

I am making a form like this

    @using (Html.BeginForm())
    {

        <div class="row">
            <div class="large-6 columns">
                <input type="text" placeholder="Meals per day" id="numOfMealsPerDay" value="@Model.numberOfMeals" />
               <input type="hidden" name="myHiddenInput" id="myHiddenInput" value="@Model.Id" />
            </div>
        </div>
        <div class="row">
            <div class="large-6 columns">
                <button type="submit" class="button small" id="updateNumOfMeals">Submit</button>
            </div>
        </div>
    }

In my Jquery I am doing this

$('form').submit(function () {

    var numOfMealsPerDay = $('#numOfMealsPerDay').val();

    console.log("form submitted");

    if (numOfMealsPerDay != '' && numOfMealsPerDay > 0) {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                console.log(result);
            }
        });
    } else {
        alert('cannot be empty or less than 0');
    }


    return false;
});

In the controller I am doing like this

   [HttpPost]

    public ActionResult UpdateSettings()
    {

        Debug.WriteLine("1");
        return Content("Thanks", "text/html");
    }
12
  • 1
    Why use both [HttpPost] and [HttpGet] attribute for the same action method? Commented Nov 26, 2014 at 12:54
  • @AdrianoRepetti it doesn't even go to my controller action. I am trying to Debug it, the code never reached there. Commented Nov 26, 2014 at 12:58
  • Where UpdateSettings() with proper parameters is? I'm not sure routing will pick parameterless method as last choice. Commented Nov 26, 2014 at 13:01
  • 1
    @mohsinali1317: You can avoid having the page being a result of a POST request by returning a Redirect from the action method that handles the POST request. Then the browser will do a GET request for the URL that you specify in the redirect. Commented Nov 26, 2014 at 13:21
  • 1
    @mohsinali1317: The concept is called Post/Redirect/Get (easier to search for when you know that) and there are a lot of examples out there. Commented Nov 26, 2014 at 13:46

2 Answers 2

1

The action name is specified by the route that is handling the request. If you haven't specified any routes you will be using the default route that catches an url in the form /Controller/Action.

As you don't specify any action and controller in the BeginForm method, it will use the same action and controller as the current page. You can use View Source in the browser to check that the generated form tag has the correct URL in the action attribute.

If the page is for example /Meals/UpdateSettings then the form tags action attribute will be the same, so the page will be posted back to the same address but with the POST http method instead of GET.

For that URL the UpdateSettings action in the MealsController controller would be used.

Sign up to request clarification or add additional context in comments.

Comments

0

Do I have to mention the action name in the submit event of the form or will it figure it automatically?

You don't have to specify it if the HttpPost action name is the same as the HttpGet one. If you have a different action name you have to specify it like this:

Html.BeginForm(string ActionName, string ControllerName)

You also have other 13 definitions than you can use if you need to specify additional details (route values, method, html attributes, etc...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.