I have a question about Ajax form submit. Here is the code:
Controller
[HttpPost, ValidateAntiForgeryToken, ValidateInput(true)]
public ActionResult Contact(ContactForm model)
{
if (!ModelState.IsValid)
{
return new EmptyResult();
}
else
{
string message = model.Name + " " + model.Telephone + " " + model.Email + " " + model.Address + " " + model.City + " " + model.ZipCode;
//send it
return new EmptyResult();
}
}
View
<script type="text/javascript">
function PostSuccess() {
$('.success-message').html("Thank you for your interested");
}
function PostFailure() {
$('.success-message').html("Something went wrong... Make sure the required fields are filled out and in correct format");
}
</script>
<div class="contact-form">
@using (@Ajax.BeginForm("Contact", "Info", new AjaxOptions { HttpMethod = "Post", OnFailure = "PostFailure", OnSuccess = "PostSuccess" }))
{
<p>
@Html.LabelFor(x => x.Name, "Your name")
@Html.TextBoxFor(x => x.Name, new { @class = "required" })<span class="required">*</span>
</p>
<p>
@Html.LabelFor(x => x.Email, "Your email")
@Html.TextBoxFor(x => x.Email)<span class="required">*</span>
</p>
<p>
@Html.LabelFor(x => x.Telephone, "Your phone")
@Html.TextBoxFor(x => x.Telephone)<span class="required">*</span>
</p>
<p>
@Html.LabelFor(x => x.Address, "Your address")
@Html.TextBoxFor(x => x.Address)
</p>
<p>
@Html.LabelFor(x => x.ZipCode, "Your zipcode")
@Html.TextBoxFor(x => x.ZipCode)
</p>
<p>
@Html.LabelFor(x => x.City, "Your city")
@Html.TextBoxFor(x => x.City)
</p>
@Html.AntiForgeryToken()
<button type="submit" id="bContact">Send Message</button>
}
<div class="required">* Required Fields</div>
<div class="success-message">Output</div>
All I want is a appropriate message in "success-message" class, but the POST is always successful so I always get "Thank you for your interest."
Thank you.