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

前端 未结 3 1328
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题