I made a form with @using(Ajax.BeginForm){}
. It posts to a method in my controller that returns a PartialViewRes开发者_运维百科ult. It works fine when everything is valid, but what should I return if
it's not (e.g., the modelstate is not valid)?
How Ajax.BeginForm can manage the error? What I return to manage the Failure?
@using(Ajax.BeginForm("Create", "Room", new AjaxOptions { HttpMethod="POST",
UpdateTargetId="formRoom", InsertionMode= InsertionMode.Replace, onFailure =??})) {
public PartialViewResult Create(Movie mov)
{
if (ModelState.IsValid)
{
db.Save(mov);
return PartialView("CreateResult", mov);
}
return null;
}
Thanks!
You may return another View.
e.g. return PartialView("MyErrorView");
Just return Create action with the model to display the errors (the same as you returned on your get):
[HttpPost]
public PartialViewResult PartialCreate(Album album)
{
if (ModelState.IsValid)
{
db.Albums.Add(album);
db.SaveChanges();
return PartialView("Index");
}
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return PartialView(album);
}
精彩评论