开发者

Exporting ViewData\ModelState to Child Action

开发者 https://www.devze.com 2023-03-27 11:40 出处:网络
As the subject says, is this a bad idea? If so, why? Cu开发者_开发问答rrently, if you are rendering some input fields from inside a @Html.Acion, the validation errors don\'t get displayed to the user

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

Cu开发者_开发问答rrently, 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号