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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:43:03+00:00 2026-06-13T03:43:03+00:00

In my MVC 4 project, I have a Model like: public class MyModel {

  • 0

In my MVC 4 project, I have a Model like:

public class MyModel
{
    private readonly IService MyService;

    public MyModel([Dependency]IService service)
    {
        this.MyService = service;
    }
    // ...
}

My Controller has an action which is an HttpPost like:

[HttpPost]
public ActionResult Show(MyModel myModel)
{
    // do stuff

My Controller does not (and should not) have an instance of my IoC container.
When the HttpPost happens and the action is called, MVC tries to create an instance of my Model when it does the model binding. It needs a parameterless constructor to do so. If it creates a Model using the default constructor, MyService won’t be set, and the Model will not have all of it’s dependencies present. I don’t want to have to call myModel.Init(someIServiceInstance). I want to plug into the framework so that the dependency is resolved (the correct constructor is called) at the creation of the model.

I’m currently using Unity as my dependency resolver and have a few factories for things like Controllers, Service clients, etc. For the controller, I was able to register the ControllerFactory and pass dependencies into the controllers, but I don’t see a ModelFactory that I can register. I’m not so concerned with the binding process itself; once the model is instantiated, I’m happy with how the binding is done. I only want to implement how the model is created/constructed. I’m also okay if the ModelFactory uses a reference to the container in order to resolve the dependency (I can inject the container to the factory as a parameter to it’s constructor, I just don’t want the Controllers to have a reference directly to the container).

Does anyone have a sample of a ModelFactory (or ModelBinder, and/or ModelBinderResolver, etc.) that will handle creation of the Model, and then rely on the frameworks implementation to do the rest of the Binding process?

  • 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-06-13T03:43:04+00:00Added an answer on June 13, 2026 at 3:43 am

    You could write a custom model binder:

    public class MyModelModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            // TODO: create the model instance here using your DI framework or the 
            // DependencyResolver which is more correct
        }
    }
    

    and then register this model binder in your Global.asax:

    ModelBinders.Binders.Add(typeof(MyModel), new MyModelModelBinder());
    

    But usually this is bad practice. I would recommend you using view models and have your controller actions take view models as parameters and pass view models to the views. Those view models will have parameterless constructor. And then you could have your domain model or repository injected into the controller constructor.

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

Sidebar

Related Questions

With ASP.net MVC I have a Model like so: public class Project { public
I have an MVC project similar to this... Model Public Class ItemDetails Public Property
I have an ASPNET MVC 2 project. When I use <%= Html.TextBoxFor(model => model.Login)
I am using MVC-Viewmodel, EF model first on my project. I have 3 DropDonLista
Im using MVC-Viewmodel, EF Model first on my project. I have a foreach loop
I have an MVC project in .Net 4 with WCF service with BasicHttpBinding. When
I have an MVC project that I would like to write in .NET 4.0
I have created an OrderFormViewModel which looks something like public class OrderFormViewModel { public
Using MVC 2 I have been trying to make this record store project. Creating
This is for .net MVC project i'm working on I have the following classes:

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.