导出的ViewData \\ ModelState中以儿童行动 [英] Exporting ViewData\ModelState to Child Action

查看:134
本文介绍了导出的ViewData \\ ModelState中以儿童行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为题称,这是一个坏主意?如果是这样,为什么?

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

目前,如果你是渲染从@ Html.Acion里面的一些输入字段,验证错误没有得到显示给用户的ModelState中时,@ Html.Action介入被清除(在其上下文中)。

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.)

因此​​,whats它周围的最佳模式?

So whats the best pattern around it?

推荐答案

您可以通过访问 ControllerContext.ParentActionViewContext ViewContext.ParentActionViewContext父上下文(详细信息<一个href=\"http://stackoverflow.com/questions/4412310/how-to-get-current-controller-and-action-from-inside-child-action\">here)但我认为有更好的解决方案。

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.

这篇关于导出的ViewData \\ ModelState中以儿童行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆