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

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

提交回复
热议问题