减少使用HTML佣工code复制 [英] Reducing code duplication using HTML helpers

查看:141
本文介绍了减少使用HTML佣工code复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP MVC写了一个相当简单的审计数据的Web应用程序,有效地对不同的目的相同型号的两个视图。

I have a fairly simple data audit web application written with ASP MVC which effectively has two views of the same model for different purposes.


  1. 代理视图 - 通过验证信息的信息的人填写该表格。该视图中的表单上的每个字段有3子字段:

  1. Agent view - The form filled out by the person validating information the information. Each field on the form in this view has 3 subfields:

一个。原值 - 从数据库中的值调用制作前

a. Original Value - The value from the database before the call was made

乙。新值 - 如果它从原始的不同的电话上的人提供的值

b. New Value - The value provided by the person on the phone if it differs from the original.

℃。操作 - 发生了什么事的一般指示

c. Action - A general indication of what happened

QC视图 - 被人谁回顾了代理视图下的工作填写该表格。该视图中的表单上的每个字段有5子字段:

QC View - The form filled out by someone who reviews the work performed in Agent view. Each field on the form in this view has 5 subfields:

一个。原值 - 同上

乙。代理值 - 由代理1B提供上述值

b. Agent Value - The value provided in 1b above by the agent.

℃。 QC价值 - 修正后的新价值,如果由代理人指定的值是不正确

c. QC Value - The corrected "New Value" if the value specified by the agent is incorrect.

Ð。代理操作 - 同上,但只在该视图读

d. Agent Action - Same as above, except read only in this view

即QC行动 - 修正后的新动作,如果不适当地由代理选择

e. QC Action - The corrected "New Action" if improperly chosen by the agent.

这两种观点之间的唯一区别是可用的子域。我希望能够使用一个单一的视图来重新present两种观点,因为页面的整体结构是相同的,只是使用HTML佣工来处理子场的差异。我至今2个完全独立的系列佣工(目前在同一类,但是可以分隔):

The only differences between the two views are the subfields available. I'd like to be able to use a single view to represent both views since the overall structure of the pages is identical, and just use HTML helpers to handle the differences in subfields. What I have so far are 2 distinctly separate series of helpers (currently in the same class though could be separated):

// Agent controls
public static MvcHtmlString AuditControl(this HtmlHelper htmlHelper, string id, string fieldLabel, MvcHtmlString editControl, string cssClass)
public static MvcHtmlString AuditControl(this HtmlHelper htmlHelper, string id, string fieldLabel, string editControl, string cssClass)
public static MvcHtmlString AuditControl<COMPLEX>(this HtmlHelper htmlHelper, string id, string fieldLabel, string cssClass) where COMPLEX : AbstractComplex, new()

// QC controls
public static MvcHtmlString ReviewControl(this HtmlHelper htmlHelper, string id, string fieldLabel, MvcHtmlString editControl, string cssClass)
public static MvcHtmlString ReviewControl(this HtmlHelper htmlHelper, string id, string fieldLabel, string editControl, string cssClass)
public static MvcHtmlString ReviewControl<COMPLEX>(this HtmlHelper htmlHelper, string id, string fieldLabel, string cssClass) where COMPLEX : AbstractComplex, new()

当第三实施方式处理多个数据(例如姓名,地址,等等)组成的更复杂的领域。

Where the third implementations handle the more complex fields composed of multiple pieces of data (like Full name, Address, etc).

这是我考虑的一个可能的解决方案是将不同类型的控件分离成实现共同的接口不同的类,然后将它们传递类型参数,更通用的HTML助手。我认为这会工作,但那么我会莫名其妙地需要能够告诉视图它实施应该用来绘制来看,这似乎是有问题的,因为它似乎模糊了视图和控制器之间的界限。

One possible solution that I've considered is to separate the different types of controls into different classes which implement a common interface and then pass them as type parameters to more generic HTML helpers. I think this would work but then I'd somehow need to be able to tell the view which implementation it should use to draw the view, which seems problematic because it seems to blur the line between View and Controller.

这似乎是显而易见的少了一个吸引人的方法是从通过一个通用的使用(在逻辑上没有意思型通用)工厂辅助控制器通过某种管理标志,并建立它知道哪些一系列的方法逻辑去用。这将保持模型和视图分开的,但感觉脏的,因为这样的HTML帮助将成为负责的不仅仅是构建HTML多。

One less appealing approach that seems obvious is to pass a sort of admin flag from the controller which would be used by a generic (in logic not meaning type generic) factory helper and build the logic in it to know which series of methods to use. This would keep the model and view separate, but feels dirty because then the HTML helper would become responsible for more than just building the HTML.

这是一个合理的局面,打破由MVC设计还是有一个更合适的解决方案?

Is this a reasonable situation to break the separation of concerns as designed by MVC or is there a more appropriate solution?

推荐答案

由于您使用MVC3,我会建议使用童工行为的子领域:

Since you are using MVC3, I would recommend using child actions for the sub fields:

http://haacked.com/archive/2009/ 11月18日/ aspnetmvc2渲染-action.aspx

儿童的行为让你执行你的观点的内部控制器上的操作,这将是一个更清洁的方式。

Child actions allow you to execute an action on a controller inside of your view, this would be a much cleaner approach.

这篇关于减少使用HTML佣工code复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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