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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:32:15+00:00 2026-05-20T22:32:15+00:00

I was researching something and came across this blog post at buildstarted.com about model

  • 0

I was researching something and came across this blog post at buildstarted.com about model binders. It actually works pretty darn well for my purposes but I am not sure exactly whats going on behind the scenes. What I did was create a custom ModelBinder called USerModelBinder:

public class UserModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        ValueProviderResult value = bindingContext.ValueProvider.GetValue("id");
        MyEntities db = new MyEntities();
        User user = db.Users.SingleOrDefault(u => u.UserName == value.AttemptedValue);
        return user;
    }
}

Then in my Global.asax.cs I have:

ModelBinders.Binders.Add(typeof(User), new UserModelBinder());

My understanding is that using the model binder allows me to NOT have to use the following lines in every controller action that involves a “User”. So instead of passing in an “id” to the action, the modelbinder intercepts the id, fetches the correct “item”(User in my case) and forwards it to the action for processing.

        MyEntities db = new MyEntities();
        User user = db.Users.SingleOrDefault(u => u.UserName == value.AttemptedValue);

I also tried using an annotation on my User class instead of using the line in Global.asax.cs:

[ModelBinder(typeof(UserModelBinder))]
public partial class User
{
}

I’m not looking for a 30 page white paper but I have no idea how the model binder does what it does. I just want to understand what happens from when a request is made to the time it is served. All this stuff “just working” is not acceptable to me, lol. Also, is there any difference between using the annotation versus adding it in Global.asax.cs? They seem to work the same in my testing but are there any gotchas?

  • 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-20T22:32:15+00:00Added an answer on May 20, 2026 at 10:32 pm

    Usually the Model Binder (in MVC) looks at you Action method and sees what it requires (as in, the objects types). It then tries to find the values from the HTTP Request (values in the HTTP Form, QueryString, Json and maybe other places such as cookies etc. using ValueProviders). It then creates a new object with the parameters that it retrieves.

    IMO What you’ve done is not really “model binding”. In the sense that you’ve just read the id and fetched the object from the DB.

    example of usual model binding:

    // class
    public class SomeClass
    {
        public int PropA {get;set;}
        public string PropB {get;set;}
    }
    
    // action
    
    public ActionResult AddSomeClass(SomeClass classToBind)
    {
      // implementation
    }
    
    // pseudo html
    
          <form action="">
           <input name="PropA" type="text" />
           <input name="PropB" type="text" />
          </form>
    

    if you post a form that contains the correct values (lets say you post a form with PropA and PropB ) the model binder can identify that you’ve sent those values in the form and build a SomeClass object.

    If you really want to create a real working example you should use a strongly typed View and use HtmlHelper’s EditorFor (or EditorForModel) to create all the correct names that MVC needs.

    —

    for reference MVC’s default binder is the DefaultModelBinder, and some (there are more, you can look around in the System.Web.Mvc namespace) ValueProviders that it uses by default are the FormValueProvider and the QueryStringValueProvider

    So, as I already said, how this basically works is that the default model binder reads the model that the action is recieving (say SomeClass in the example) reads what are the values that it can read (say PropA and PropB) and asks the ValueProviders for the correct values for the properties.

    Also, if I recall correctly, you can also see the value providers in runtime using the ValueProviderFactories static class.

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

Sidebar

Related Questions

While researching the issue of JSON vs XML , I came across this question
I came across this really nice looking Scala code while researching XMPP for a
This is something that I came across in the Apple reference material - Sample
This post started as a question on ServerFault ( https://serverfault.com/questions/131156/user-receiving-partial-downloads ) but I determined
Sorry for the very involved question, but this is something I've been researching for
I've been researching on how to do this for about a week now and
Hey guys, I'm researching something for a friend of mine who's involved in road
Is something like this possible to do in Java? for (Object o : objects)
I'm researching this for a project and I'm wondering what other people are doing
EDIT: After spending several hours researching this, I don't think I'm going to find

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.