2
votes

I got an aspx page call ListArticles with the following code :

<% Html.RenderPartial("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 ?

3
Use Html.RenderAction instead??Martin
Agreed with Martin. Using Html.RenderAction will encapsulate all of this functionality into its own custom Controller/View combination. You can then do within this custom controller as you see fit.Robert Harvey
I try to use a Html.RenderAction instead and got the same problem. Do you have any example or link that shows how to do it (with a create) ?Melursus

3 Answers

1
votes

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.

0
votes

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.

0
votes

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