Code duplication in MVC controllers

I think I have a problem understanding the correct way to use MVC. The problem I am facing is that I have users and admin users who are allowed to create a campaign, but they use different master pages, etc. Etc.

My solution so far ...

Controllers AdminUserController.cs UserController.cs Views AdminUser CreateCampaign.aspx User CreateCampaign.aspx 

But in doing so, I have to duplicate the CreateCampaign () code in both the AdminUserController and the UserController, and I have 2 views to do the same.

Is this the right way to do this, or am I missing a ship somewhere?

+4
source share
6 answers

Extract the common code to the base controller from which both are inherited. Extract the general view into a general, general partial view (ViewUserControl), then each view includes this general partial view. The latter is really necessary, since your presentation 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" ); %> 
+9
source

You can do just fine with one controller, leave it in the UserController. The administrator is another user, right? In the CreateCampaign () code, you can check the "special" status of the registered user and set additional properties before saving the data.

Is it possible to get away with a general idea depends on how much they differ. You can use the simple IsAdmin () checks in the view to render or not have additional controls. Or you can check it in the controller and serve one kind.

+2
source

Why do you have two different "Users"? I would prefer one user class and roles to provide access to different views / actions.

Then you have to create Campain-Controller and create CreateCampaign-Action.

+1
source

You are missing a partial boat :)

You can create one CreateCampaign.ascx file that shares the common view code and calls it a partial view in some other view. Instead of creating full views, create a partial view (.ascx file) and save a duplicate of the code and markup.

Then you can reuse it in other views with this:

 <% Html.RenderPartial("CreateCampaign") %> 

Reuse your controller code by dividing it into some base controller that your specific controllers inherit from.

0
source

How about CampaignController has a Create method, which then displays different views depending on the type of user. Sort of:

 public class CampaignController : Controller { public ActionResult Create() { //... if (User.IsInRole("Admin") { Return View("AdminUser/CreateCampaign"); } else { Return View("User/CreateCampaign"); } } } 

And, as others say, duplicate markup / code in views should be divided into partial views, and then RenderPartial ():

 <% Html.RenderPartial("Campaign") %> 
0
source

If it is as simple as displaying another wizard for users who are admins, you can set the main page for presentation from the controller as follows: -

 return View("CreateCampaign", User.IsInRole("Admin") ? "Admin", "User"); 

This should allow you to have one Campaign controller and Create view, which seems more natural to me than controllers designed for a specific type of user (which looks like implementation details).

0
source

All Articles