ASP.NET MVC - 如何实现可重复使用的用户控制和保持干? [英] ASP.NET MVC - How to achieve reusable user controls and maintain DRY?

查看:146
本文介绍了ASP.NET MVC - 如何实现可重复使用的用户控制和保持干?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一篇文章,所以请温柔:)

First post so please be gentle :)

在ASP.NET MVC中创建用户控件,什么是结构化code,这样的调用使用该用户控件不都享有控制器知道这么多关于控件的最好方法?我想知道,同时使用ASP.NET MVC用户控件保持干的好办法。

When creating user controls in ASP.NET MVC, what is the best way to structure the code so that the controllers that invoke views that use the user controls do not all have to know so much about the controls? I would like to know a good way to maintain DRY while using user controls in ASP.NET MVC.

请注意,这个问题只涉及到需要回发的特殊处理和逻辑用户控件。我没有问题,创造美好DRY code对于那些只(使用的RenderPartial),或者需要一些pre-处理创建相应的视图模型(使用的RenderAction)任一视图用户控件。

Please note, this question only pertain to user controls that require special handling and logic on a postback. I have no problem creating nice DRY code for user controls that are either view only (using RenderPartial) or that require some pre-processing to create the appropriate ViewModel (using RenderAction).

此外,此问题只适用于在应用程序中实现可重用的控件。我不担心在这一点上的应用程序之间的可重用性。

Also, this question pertains only to achieving reusable controls within an application. I am not worried about reusability between applications at this point.

要给出一个具体的例子,让我们说,我想创建一个包含三个输入字段,名字,姓氏和公司名称和一个提交按钮快速添加用户控件。当使用快速添加功能,下面的步骤应执行独立的控制是在什么页面:

To give a specific example, let's say I would like to create a 'Quick Add' user control which contains three entry fields, First Name, Last Name and Company Name and a submit button. When the QuickAdd functionality is used, the following steps should be performed independent of what page the control is on:


  1. 验证该领域是不是空的,如果是这样,显示出一个指标。

  2. 执行的查找到存储库以查看是否公司已经存在,如果没有;创建它。

  3. 创建关联到现有的公司或新成立的公司一个新的联系人

  4. 重新呈现现有页面。如果没有验证错误,用户将再次看到完全一样的页面,否则同一页面验证错误。

我的主要问题与实现DRY与所有调用包含局部视图最终不得不有一个动作方法来处理来自快速添加表单提交意见的控制器做。即使我从其他每个控制器打出来的逻辑处理信息到一个单独的控制器,并调用该方法似乎是一个负担每个控制器调用具有可重用的控件的观点必须有知识。

My main problem with achieving DRY has to do with all the controllers that invoke views that contain the partial view end up having to have an Action Method to process the form submission from the Quick Add. Even if I break out the logic for processing the information into a separate controller and invoke that method from each of the other controllers it seems like a burden that each and every controller that invoke views that have reusable controls have to have that knowledge.

我看着另一个选择是让可重复使用的控制总是向一个特定的操作方法/控制器,但那么有没有办法让该控制器知道如何适当地重新填充模型调用该特定的控制器认为包含在可重复使用的控制(步骤4)。

The other option I looked at was to have the reusable control always submit to a specific action method / controller but then there is no way for that controller to know how to re-populate the model appropriately for the specific controller that invoked the view that contained the reusable control (in step 4).

据我所知,没有在MVC 2子控制器的通话(此问题<一href=\"http://stackoverflow.com/questions/1244911/asp-net-mvc-contained-user-controls\">http://stackoverflow.com/questions/1244911/asp-net-mvc-contained-user-controls)但因为它现在还没有,什么是结构化code,以实现最大的可重用性,同时保持干的最好方法?

I am aware that there is talk of subcontrollers in MVC 2 (from this question http://stackoverflow.com/questions/1244911/asp-net-mvc-contained-user-controls) but since it is not there yet, what is the best way to structure the code to achieve maximum reusability while maintaining DRY?

是否有需要在所有的调用使用可重复使用的控件(与上述的特点之一)的意见控制器,必须有一个动作方法来处理从控制信息的方法吗?

Is there an alternative to having to have all the controllers that invoke views that use a reusable control (with the characteristics of the one described above), having to have an Action Method to process the information from the control?

推荐答案

在你的帖子,你问的的结尾是否有需要在所有的控制器替代...必有的操作方法从控制处理信息

有关这个问题的答案是写一个自定义模型绑定即可。您的自定义模型绑定可以负责从所有控制器使用传入表单控件(S)为模型或属性的填充值。通常情况下,你想单独从模型验证约束力,但没有任何理由,你不能将它们合并为好。

The answer for that question is to write a custom model binder. Your custom model binder can be responsible for the populating the values from the incoming form control(s) into model or properties used by all of the controllers. Normally, you want to separate the validation from the model binding, but there is no reason that you couldn't combine them as well.

我强烈推荐的 6提示ASP.NET MVC模型的一些很好的参考沿绑定为主题的深入讨论

这篇关于ASP.NET MVC - 如何实现可重复使用的用户控制和保持干?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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