Asp.Net Mvc - Render partial view - Manage errors
I got an aspx page call ListArticles with the following code :
<% Html.RenderP开发者_开发问答artial("Create", new Models.Article()); %>
Create is a partial view (Create.ascx).
In my controller, I got something like this :
if (!ModelState.IsValid) {
return View();
}
So the problem is that the view generated by return View();
doesn't render the good view. It's should render the ListArticles view while highlighting errors in the Create partial view but it's only show the Create.ascx view.
Is there a way to handle that ?
I suggest for this situation which you embed a form in your View which need to post and show the errors you use Ajax.BeginForm instead of partial views . Partial Views is more suitable for showing scenarios.
Are you sure you've provided all the code?
in your controller you're returning a view but not passing a model to it.
so you need Return View(Articles) or something like that. On error you still need to return the collection, or model, that you used to render the view in the first place.
You may have the action name as "Create", so thats the reason it is showing only Create.ascx view. Try putting the following code instead
if (!ModelState.IsValid) {
return View("ListArticles");
}
You should have Html.ValidationMessage() in your Create.ascx to see the validation errors
精彩评论