ASP.NET MVC - 如何实现可重用的用户控件并保持 DRY?

ASP.NET MVC - How to achieve reusable user controls and maintain DRY?(ASP.NET MVC - 如何实现可重用的用户控件并保持 DRY?)
本文介绍了ASP.NET MVC - 如何实现可重用的用户控件并保持 DRY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次发帖,请温柔:)

First post so please be gentle :)

在 ASP.NET MVC 中创建用户控件时,构建代码的最佳方式是什么,以便调用使用用户控件的视图的控制器不必对控件了解太多?我想知道在 ASP.NET MVC 中使用用户控件时保持 DRY 的好方法.

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.

请注意,此问题仅适用于在回发时需要特殊处理和逻辑的用户控件.我可以为仅查看(使用 RenderPartial)或需要一些预处理来创建适当的 ViewModel(使用 RenderAction)的用户控件创建漂亮的 DRY 代码.

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.

举一个具体的例子,假设我想创建一个快速添加"用户控件,其中包含三个输入字段,名字、姓氏和公司名称以及一个提交按钮.使用 QuickAdd 功能时,应独立于控件所在的页面执行以下步骤:

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 中有关于子控制器的讨论(来自这个问题 ASP.NET MVC - 包含用户控件),但由于它还没有,那么在保持 DRY 的同时构建代码以实现最大可重用性的最佳方法是什么?

I am aware that there is talk of subcontrollers in MVC 2 (from this question 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?

推荐答案

在你的帖子最后,你问除了必须拥有所有控制器之外,还有其他选择吗......必须拥有一个 Action Method处理来自控件的信息"

At the end of your post, you ask "Is there an alternative to having to have all the controllers... having to have an Action Method to process the information from the control"

该问题的答案是编写一个自定义模型绑定器.您的自定义模型绑定器可以负责将传入表单控件的值填充到所有控制器使用的模型或属性中.通常,您希望将验证与模型绑定分开,但也没有理由不能将它们结合起来.

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.

我强烈推荐 ASP.NET MVC 模型绑定的 6 个技巧 以深入讨论该主题以及一些很好的参考资料.

I highly recommend 6 Tips for ASP.NET MVC Model Binding for a deeper discussion of the topic along with some good references.

这篇关于ASP.NET MVC - 如何实现可重用的用户控件并保持 DRY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)