Duplicating code in MVC controllers
asp.net-mvc
Solution
Extract the common code to a base controller from which both inherit. Extract the shared view to a common, shared partial view (ViewUserControl), then have each view include this shared partial. The latter is really only necessary since your view uses different master pages.
Controllers:
BaseUserController
CreateCampaign()
UserController : BaseUserController
AdminController : BaseUserController
Views:
Shared
CreateCampaignShared.ascx
Admin.Master
User.Master
Admin
CreateCampaign.aspx -- includes <% Html.RenderPartial( "CreateCampaignShared" ); %>
User
CreateCampaign.aspx -- includes <% Html.RenderPartial( "CreateCampaignShared" ); %>
Problem
I think I have a problem in understanding the proper way of using MVC. The problem I'm having is that I have Users and Admin Users, both are allowed to create a campaign but they are using different master pages etc etc. My solution so far is... ``` Controllers AdminUserController.cs UserController.cs Views AdminUser CreateCampaign.aspx User CreateCampaign.aspx ``` But in doing it in this way I'm having to duplicate the CreateCampaign() code in both the AdminUserController and the UserController and I have 2 views to do the same thing. Is this the right way of doing it or am I missing the boat somewhere?