I'm practicing Ajax! I have a simple contact form and this is my actions :
public ActionResult Contact()
{
return View("Contact");
}
[HttpPost]
public ActionResult Contact(ContactViewModel contactViewModel)
{
if (ModelState.IsValid)
{
var contact = contactViewModel.ConvertToContactModel();
_contactRepository.Add(contact);
_contactRepository.Save();
return Json(new { msg = "Your contact Sent, I'll response soon." });
}
return Json("Sorry! Somthing went wrong, try again or contact again");
}
and this is my View :
@model Blog.Web.UI.ViewModels.ContactViewModel
@{
ViewBag.Title = "Contact";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="Contact">
@using (Ajax.BeginForm("Contact", "Home", new AjaxOptions() { OnSuccess = "Success" }))
{
<div>
@Html.LabelFor(c => c.Name)
@Html.TextBoxFor(c => c.Name)
</div>
<div>
@Html.LabelFor(c => c.Email)
@Html.TextBoxFor(c => c.Email)
@Html.ValidationMessageFor(c => c.Email)
</div>
<div>
@Html.LabelFor(c => c.Subject)
@Html.TextBoxFor(c => c.Subject)
</div>
<div>
@Html.LabelFor(c => c.Body)
@Html.TextAreaFor(c => c.Body)
@Html.ValidationMessageFor(c => c.Body)
</div>
<input type="submit" value="Send" />
}
</div>
<script type="text/javascript">
function Success(context) {
if (context[0]) {
$("#Contact").empty().html(context[1]);
}
}
</script>
Now I wanna to show the user success or failing of contact made , what's the problem of my code that doesn't work?? it is so interesting that my validation doesn't work in this case! please help me about this , thanks