Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:17:19+00:00 2026-05-28T06:17:19+00:00

In MVC 3, I’ve been refactoring my controllers to remove some duplicate code by

  • 0

In MVC 3, I’ve been refactoring my controllers to remove some duplicate code by using generics which I was successful in doing, but now I’m trying to refactor my views, but I can’t seem to adjust the model that the view uses to use generics.

Here is the relevant portion of my refactored abstract base controller:

public abstract class CrudController<TEntity> : Controller where TEntity : Entity, INamedEntity, IOrderedEntity, IHasMembers, new()
{
  private readonly IUnitOfWork _unitOfWork;
  protected readonly IRepository<TEntity> Repository;

  protected CrudController(IUnitOfWork unitOfWork)
  {
    _unitOfWork = unitOfWork;
    Repository = _unitOfWork.RepositoryFor<TEntity>();
  }

  public ActionResult Edit(int id)
  {
    return PartialView(Repository.FindById(id));
  }

  // More actions here...
}

Here are my actual controllers (they are really basic since the refactoring):

public class StatusController : CrudController<MemberStatus>
{
  public StatusController(IUnitOfWork unitOfWork) : base(unitOfWork)
  {
  }
}

public class PositionController : CrudController<MemberPosition>
{
  public PositionController(IUnitOfWork unitOfWork) : base(unitOfWork)
  {
  }
}

Note that both MemberStatus and MemberPosition implement ‘Entity, INamedEntity, IOrderedEntity, IHasMembers’

Now here are my Edit views for those two controllers which I want to refactor:

@model MyApp.Domain.Entities.MemberStatus
@using (Html.BeginForm("Save", @ViewContext.RouteData.Values["Controller"].ToString(), FormMethod.Post, new { id = "EntityForm" }))
{
  @Html.Hidden("Id")
  <label class="Name">
    <span>New Name:</span><br />
    @Html.EditorFor(x => x.Name)
  </label>
}

@model MyApp.Domain.Entities.MemberPositions
@using (Html.BeginForm("Save", @ViewContext.RouteData.Values["Controller"].ToString(), FormMethod.Post, new { id = "EntityForm" }))
{
  @Html.Hidden("Id")
  <label class="Name">
    <span>New Name:</span><br />
    @Html.EditorFor(x => x.Name)
  </label>
}

You can see that the views are almost identical, with the exception of the first line which declares the model. I want to find out how to pass in a generic parameter like I did with the controllers, which I thought (in my naivete) would look something like the following, but this obviously doesn’t work.

@model <TEntity> where TEntity : Entity, INamedEntity, IOrderedEntity, IHasMembers, new()

How can I refactor these two views (by putting a generic view in the Shared folder) that would implement a non-concrete model in the view? Any suggestions are appreciated.

FYI – I think (though I haven’t tried) that I could use a ViewModel, but I would prefer not to have to do that in this case (as I would have to implement the mapping from the entities to the ViewModel for each entity which somewhat defets the purpose of this refactoring).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:17:20+00:00Added an answer on May 28, 2026 at 6:17 am

    If both of your models have common properties, create an interface with those properties in the interface, then make both models implement that interface. Now create a view that’s strongly-typed to that interface. Another option is simply just a weakly-typed view that is dynamically evaluated at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MVC and trying to use dependency injection for controllers, but when I try
MVC 3. Vb.net. Part of my app generates PDF files using Itextsharp. Some strings
Duplicate MVC .NET For the Desktop? I've developed some ASP.NET MVC apps, and loving
Using MVC with an observer pattern, if a user action requires polling a device
Which MVC-framework is the best option (performance/ease of development) for a web application, that
My mvc page returns json by using the function Json(mycustomclass) to return a JsonResult
MVC is formatting my code horribly, and I was wondering if you can turn
MVC 2 comes with built-in support of Model validation but how do I do
Does MVC 3 have a way to automatically scaffold controllers and views for my
In MVC I am using [Required(ErrorMessage=)] to validate my text. How do I utilize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.