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");
}
[HttpPost]and[HttpGet]attribute for the same action method?Redirectfrom 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.