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 6863643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:48:31+00:00 2026-05-27T02:48:31+00:00

I want to have one model & view that is served by multiple controllers

  • 0

I want to have one model & view that is served by multiple controllers in my ASP.NET MVC 3 app.

I’m implementing a system that interacts with the users’ online calendar and I support Exchange, Google, Hotmail, Yahoo, Apple, ect… Each of these has wildly different implementations of calendar APIs, but I can abstract that away with my own model. I’m thinking that by implementing the polymorphism at the controller level I will be able to deal cleanly with the different APIs and authentication issues.

I have a nice clean model and view and I’ve implemented two controllers so far that prove I can read/query/write/update to both Exchange and Google: ExchangeController.cs and GoogleController.cs.

I have /Views/Calendar which contains my view code. I also have /Models/CalendarModel.cs that includes my model.

I want the test for which calendar system the user is using to happen in my ControllerFactory. I’ve implemented it like this:

public class CustomControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == typeof(CalendarController))
        {                
            if(MvcApplication.IsExchange) // hack for now
                return new ExchangeController();
            else
                return new GoogleController();
        }
        return base.GetControllerInstance(requestContext, controllerType);
    }
}

and in my Application_Start:

ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());

This works. If I got to http://.../Calendar this factory code works and the correct controller is created!

This worked beautifully and I did it without really understanding what I was doing. Now i think I got it but I want to make sure I’m not missing something. I really spent time searching for something like this and didn’t find anything.

One thing that concerns me is that I figured I’d be able to have an inheritance relationship between CalendarController and ExchangeController/GoogleController like this:

public class ExchangeController : CalendarController
{

But if I do that I get:

The current request for action 'Index' on controller type 'GoogleController' is ambiguous between the following action methods:
System.Web.Mvc.ViewResult Index(System.DateTime, System.DateTime) on type Controllers.GoogleController
System.Web.Mvc.ActionResult Index() on type Controllers.CalendarController

Which bums me out because I wanted to put some common functionality on the base and now I guess I’ll have to use another way.

Is this the right way to do have multiple controllers for one view/model? What else am I going to have to consider?

EDIT: More details on my impl

Based on the responses below (thanks!) I think I need to show some more code to make sure you guys see what I’m trying to do. My model is really just a data model. It starts with this:

/// <summary>
/// Represents a user's calendar across a date range.
/// </summary>
public class Calendar
{
    private List<Appointment> appointments = null;

    /// <summary>
    /// Date of the start of the calendar.
    /// </summary>
    public DateTime StartDate { get; set; }

    /// <summary>
    /// Date of the end of the calendar
    /// </summary>
    public DateTime EndDate { get; set; }

    /// <summary>
    /// List of all appointments on the calendar
    /// </summary>
    public List<Appointment> Appointments
    {
        get
        {
            if (appointments == null)
                appointments = new List<Appointment>();
            return appointments;
        }
        set { }
    }


}

Then my controller has the following methods:

public class ExchangeController : Controller
{
    //
    // GET: /Exchange/
    public ViewResult Index(DateTime startDate, DateTime endDate)
    {
        // Exchange specific gunk. The MvcApplication._service thing is a temporary hack
        CalendarFolder calendar = (CalendarFolder)Folder.Bind(MvcApplication._service, WellKnownFolderName.Calendar);

        Models.Calendar cal = new Models.Calendar();
        cal.StartDate = startDate;
        cal.EndDate = endDate;

        // Copy the data from the exchange object to the model
        foreach (Microsoft.Exchange.WebServices.Data.Appointment exAppt in findResults.Items)
        {
            Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service, exAppt.Id);
            Models.Appointment appt = new Models.Appointment();
            appt.End = a.End;
            appt.Id = a.Id.ToString();

... 
        }

        return View(cal);
    }

    //
    // GET: /Exchange/Details/5
    public ViewResult Details(string id)
    {
...
        Models.Appointment appt = new Models.Appointment();
...
        return View(appt);
    }


    //
    // GET: /Exchange/Edit/5
    public ActionResult Edit(string id)
    {
        return Details(id);
    }

    //
    // POST: /Exchange/Edit/5
    [HttpPost]
    public ActionResult Edit(MileLogr.Models.Appointment appointment)
    {
        if (ModelState.IsValid)
        {
            Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service, new ItemId(appointment.Id));

           // copy stuff from the model (appointment)
           // to the service (a)
           a.Subject = appointment.Subject            

...
            a.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendToNone);

            return RedirectToAction("Index");
        }
        return View(appointment);
    }

    //
    // GET: /Exchange/Delete/5
    public ActionResult Delete(string id)
    {
        return Details(id);
    }

    //
    // POST: /Exchange/Delete/5
    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(string id)
    {
        Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service, new ItemId(id));
        a.Delete(DeleteMode.MoveToDeletedItems);
        return RedirectToAction("Index");
    }

So it’s basically the typical CRUD stuff. I’ve provided the sample from the ExchangeCalendar.cs version. The GoogleCalendar.cs is obviously similar in implementation.

My model (Calendar) and the related classes (e.g. Appointment) are what get passed from controller to view. I don’t want my view to see details of what underlying online service is being used. I do not understand how implementing the Calendar class with an interface (or abstract base class) will give me the polymorphism I am looking for.

SOMEWHERE I have to pick which implementation to use based on the user.

I can either do this:

  • In my model. I don’t want to do this because then my model gets all crufty with service specific code.
  • In the controller. E.g. start each controller method with something that redirects to the right implementation
  • Below the controller. E.g. as I’m suggesting above with a new controller factory.

The responses below mention “service layer”. I think this is, perhaps, where I’m off the rails. If you look at the way MVC is done normally with a database, the dbContext represents the “service layer”, right? So maybe what you guys are suggesting is a 4th place where I can do the indirection? For example Edit above would go something like this:

    private CalendarService svc = new CalendarService( e.g. Exchange or Google );

    //
    // POST: /Calendar/Edit/5
    [HttpPost]
    public ActionResult Edit(MileLogr.Models.Appointment appointment)
    {
        if (ModelState.IsValid)
        {
            svc.Update(appointment);
            return RedirectToAction("Index");
        }
        return View(appointment);
    }

Is this the right way to do it?

Sorry this has become so long-winded, but it’s the only way I know how to get enough context across…
END EDIT

  • 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-27T02:48:32+00:00Added an answer on May 27, 2026 at 2:48 am

    I think you’re on a dangerous path here. A controller should generally be as simple as possible, and only contain the “glue” between e.g. your service layer and the models/views. By moving your general calendar abstractions and vendor specific implementations out of the controllers, you get rid of the coupling between your routes and the calendar implementation.

    Edit: I would implement the polymorphism in the service layer instead, and have a factory class in the service layer check your user database for the current user’s vendor and instantiate the corresponding implementation of a CalendarService class. This should eliminate the need for checking the calendar vendor in the controller, keeping it simple.

    What I mean by coupling to the routes is that your custom URLs is what is currently causing you problems AFAICT. By going with a single controller and moving the complexity to the service layer, you can probably just use the default routes of MVC.

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

Sidebar

Related Questions

Using ASP.NET MVC & Spark, I have a view that is listing a number
I have an Article controller & model. I want to have a function that
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I have one list that I want to take a slice of, reverse that
i have one domain link text i want to know that does google crawl
I have one very general object that I want to map to a destination
I have one array. I want that array to retain its value between function
Using: ASP.NET MVC3 Ninject 2 Fluent nHibernate I have 2 databases (DB1 & DB2).
I am familiar with three layers viz. view model & controller. Now i want
I wanted to have more than one controller and view for same object/model in

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.