Action代码:
[HttpPost]
public ActionResult _CreateComment(PhotoCommentEditModel model)
{
if (!ModelState.IsValid)
return View(model);
PhotoComment comment = model.AsPhotoComment();
if (comment.Body.Contains("error"))//测试,当输入error的时候表示出错
{
WebUtility.SetStatusCodeForError(Response);
return View("_CreateComment", new PhotoCommentEditModel() { PhotoId = model.PhotoId });
}
else
{
this.photoCommentService.Create(comment);
return Json(new { comment.Id });
}
}
View代码:
@using (Html.BeginAjaxForm("_CreateComment", "HtmlHelper", FormMethod.Post,
new AjaxFormOptions().SetOnSuccessCallBack("success").SetOnErrorCallBack("error")))
{
//...
}
function error() {
alert("error");
}
function success() {
$("#PhotoComments").load("@(Url.Action("_PhotoComments", new { photoId = 1 }))");
}