4

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?

kidoman
  • 2,402
  • 5
  • 26
  • 35
  • Did you manage to solve it with child actions or partial views or in any other way? – gusztav.varga.dr Aug 17 '11 at 07:47
  • I went ahead with partial views with the extra data being tacked onto ViewBag. A acceptable compromise in my opinion instead of trying to hack Html.Action – kidoman Sep 13 '11 at 12:19

1 Answers1

5

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.

Community
  • 1
  • 1