ASP.NET MVC - How to achieve reusable user controls and maintain DRY?

前端 未结 3 1335
逝去的感伤
逝去的感伤 2021-02-07 20:22

First post so please be gentle :)

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 u

相关标签:
3条回答
  • 2021-02-07 20:46

    I'm not sure why you say the Quick Add form has to have an action method in each controller that uses it; if you wrap the Quick add functionality in a Html.BeginForm(); Html.EndForm() combo, you can have the beginform method specify the name of the action and controller, so you only need one controller.

    I understand where you are coming from; it's something I have been thinking about to. While I don't know all the answers, I have some ideas for you to consider. Every controller action method is invoked via a ControllerActionInvoker class, which you can customize. This class handles invoking all of the action methods, so here you could embed certain aspects of reusable code across all or certain action methods.

    Look into filters too, as there are a variety of filters that you can use or customize that fire for action methods that implement it. This way, code can run before and after the action method execution and result execution.

    For validation, there is already validation components built in that will prevent page submission... you could also consider XVAL which has some other nice features. The Unity framework is an IOC container framework, which dynamic injection keeps things loosely coupled and DRY, as you can inject all kinds of references.

    Also, you mentioned subcontrollers; the MVC preview has additional features you may be interested in... for instance, it has a RenderAction method that can render an action method within another action's view.

    Hopefully that helps... so what am I missing?

    0 讨论(0)
  • 2021-02-07 20:47

    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.

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

    0 讨论(0)
  • 2021-02-07 21:07

    Have a look at RenderAction and RenderPartial. Those are the canonical ways to arbitrarily inject a common control into a view.

    Use RenderPartial when you want to include the data as part of your ViewData infrastructure.

    Use RenderAction when you want the data to be separate from the ViewData infrastructure. The data will come from the controller method you specify in RenderAction.

    Check out the NerdDinner tutorials, if you haven't done so already.

    0 讨论(0)
提交回复
热议问题