我在练习阿贾克斯!我有一个简单的联系表格,这是我的行动:
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");
}
这是我的观点:
@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>
现在我想向用户展示联系成功或失败,我的代码不起作用的问题是什么?有趣的是,我的验证在这种情况下不起作用!请帮我解决这个问题,谢谢