My javacript looks like this:
namespace ContactPage {
$(() => {
//contact info
let $firstName = $('#first-name'), $lastName = $('#last-name'), $email = $('#email');
//form
let $form = $('#main-form');
$form.submit(() => {
let data = $form.serialize();
var post = $.post('/contact', data, function () {
console.log('posted');
});
post.done(function (data) {
console.log('done', data);
})
.fail(function (data) {
console.log('fail', data);
})
.always(function (data) {
console.log('always', data);
});
});
});
}
The MVC .net C# controller receives the data successfully (code shown below), however, the javascript doesn't wait for the response and the browser ends up displaying the Json. What am I missing?
[HttpPost]
public ActionResult Index(ContactModel model)
{
var cl = seretContext.ContactLogs.Create();
cl.Email = model.email;
cl.Comment = model.message;
seretContext.Set<ContactLog>().Add(cl);
seretContext.SaveChanges();
return Json(new { redirectTo = "/" });
}
And the cshtml has a submit button as below:
<div class="text-center">
<button id="orderForm" class="btn btn-success btn-lg" type="submit" >Send</button>
</div>
The browser immediately displays the raw Json...instead of receiving the Json so it can be processed.
Browser displays {"redirectTo":"/"}