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

  • Home
  • SEARCH
  • 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 8400079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:28:21+00:00 2026-06-09T21:28:21+00:00

In a nutshell, my question boils down to this: I need to implement a

  • 0

In a nutshell, my question boils down to this:

I need to implement a custom model binder in MVC4 that resolves models from container/service locator, whatever. I have implemented the following model binder:

public class ServiceLocatorModelBinder : DefaultModelBinder
{
    private readonly IServiceLocator _serviceLocator;

    public ServiceLocatorModelBinder(IServiceLocator serviceLocator)
    {
        _serviceLocator = serviceLocator;
    }

    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        var typeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".ModelType");
        var type = Type.GetType(
            (string)typeValue.ConvertTo(typeof(string)),
            true
        );
        var model = _serviceLocator.GetInstance(modelType);
        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
        return model;
    }
}

And now I would like to use this in place of the DefaultModelBinder in mvc to resolve all my models. How do I do this?

To clarify my need for doing this at all (as in general it’s considered best practice to just use concrete simple classes for view models/models), is that I’m experimenting with the concept of automatically generating proxies in place of my simple view model/poco classes throughout my application, and as such I don’t have a concrete type to bind to. The goal I’m hoping to achieve is taking the pattern of keeping models/view models simple, and enforcing it one step further by making it impossible for anyone to add any logic at all to these classes. This is where I need a container to resolve model types.

  • 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-09T21:28:22+00:00Added an answer on June 9, 2026 at 9:28 pm

    How do I do this?

    You could replace the default model binder with your custom one in Application_Start:

    IServiceLocator sl = ...
    ModelBinders.Binders.DefaultBinder = new ServiceLocatorModelBinder(sl);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll preface this question by mentioning that while I'm far from a regular expressions
So this is a follow-up to a previous question that I asked: Trying to
This question in a nutshell: what do \x04++HLMh7EjP3ILSfF\x00 and '\x01\x0b\x88\x0c\x01-\x10\x02\x06!\x05\x05#\n$\x0c\'\x0e%\x0b\x01&\x02\'\x06(\n\x00\x00' mean? Hi all, I'm
This question is a spin-off from this one . Some history: when I first
I asked this question previously I think poorly - so here in a nutshell...
My question in a nutshell: Does anyone know of a TwitterAnalyzer or TwitterTokenizer for
In a nutshell: what sort of applications would be benefit most from the dashboard
In a nutshell, I have a solution that builds fine in the IDE, and
I was reading C# 4 in a Nutshell and I've come to this piece
My controller, in a nutshell is this: chart1.SeriesCollection.Add(SC); using (MemoryStream ms = chart1.GetChartStream()) {

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.