kidoman
kidoman

Reputation: 2422

Exporting ViewData\ModelState to Child Action

As the subject says, is this a bad idea? If so, why?

Currently, if you are rendering some input fields from inside a @Html.Acion, the validation errors don't get displayed to the user as the ModelState gets cleared when the @Html.Action gets involved (in its context.)

So whats the best pattern around it?

Upvotes: 4

Views: 1314

Answers (1)

gusztav.varga.dr
gusztav.varga.dr

Reputation: 476

You can access the parent contexts via ControllerContext.ParentActionViewContext or ViewContext.ParentActionViewContext (details here) but I think there are better solutions.

A child action is a good choice when you do not want to pollute all of your view models with data which are available independent from the current controller action and view (for example a user welcome label, a navigation bar, etc.).

For other reuse scenarios like common input fields a partial view is a better approach.

However if you give more details about your current scenario I try to suggest a more specific solution.

Upvotes: 5

Related Questions