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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:57:27+00:00 2026-05-21T05:57:27+00:00

MVC3 comes out of the box with JsonValueProviderFactory() which is very handy for binding

  • 0

MVC3 comes out of the box with JsonValueProviderFactory() which is very handy for binding incoming JSON to a model. Unfortunately, I can’t figure out how to setup model contracts with names that differ from the incoming JSON. For example:

[DataContract(Name = "session")]
public class FacebookSession
{
    [DataMember(Name = "access_token")]
    public string AccessToken { get; set; }

    [DataMember(Name = "expires")]
    public int? Expires { get; set; }

    [DataMember(Name = "secret")]
    public string Secret { get; set; }

    [DataMember(Name = "session_key")]
    public string Sessionkey { get; set; }

    [DataMember(Name = "sig")]
    public string Signature { get; set; }

    [DataMember(Name = "uid")]
    public string UserId { get; set; }
}

when passing in a json object representing the facebook session, the properties secret and expires bind properly, but the rest do not because the property name is different than the json key name. I would expect that the datacontract serializer would try and bind to the name provided in the attribute, but that doesn’t appear to be the case. Does anyone have any workaround suggestions?

Edit

An example of how I would use this model:

    public ActionResult Log(int? custId, FacebookSession response)
    {       
          ViewBag.Id = response.UserId;                         
          return View();
    }
  • 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-21T05:57:27+00:00Added an answer on May 21, 2026 at 5:57 am

    I ended up using gt124’s link model binder example along with a better model binder to write my own model binding logic. It ended up looking like this:

    public interface IFilteredModelBinder : IModelBinder
        {
            bool IsMatch(Type modelType);
        }
    
    public class SmartModelBinder : DefaultModelBinder
    {
        private readonly IFilteredModelBinder[] _filteredModelBinders;
    
        public SmartModelBinder(IFilteredModelBinder[] filteredModelBinders)
        {
            _filteredModelBinders = filteredModelBinders;
        }
    
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            foreach (var filteredModelBinder in _filteredModelBinders)
            {
                if (filteredModelBinder.IsMatch(bindingContext.ModelType))
                {
                    return filteredModelBinder.BindModel(controllerContext, bindingContext);
                }
            }
    
            return base.BindModel(controllerContext, bindingContext);
        }
    }
    
    public class NewtonsoftJsonModelBinder : IFilteredModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            {
                // not JSON request
                return null;
            }
    
            var request = controllerContext.HttpContext.Request;
            request.InputStream.Position = 0;
            var incomingData = new StreamReader(request.InputStream).ReadToEnd();
    
            if (String.IsNullOrEmpty(incomingData))
            {
                // no JSON data
                return null;
            }
            object ret = JsonConvert.DeserializeObject(incomingData, bindingContext.ModelType); 
            return ret;
        }
    
        public bool IsMatch(Type modelType)
        {
            var ret = (typeof(JsonModel).IsAssignableFrom(modelType));
            return ret;
        }
    }
    

    I then used JSON.net attributes to map to the different object properties (instead of DataContracts) on the models. The models all inherited from an empty base class JsonModel.

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

Sidebar

Related Questions

Getting some very annoying behaviour from my MVC3 app. So I've got a model
i am using asp.net-mvc3 helpers and the jquery validation plugin (what comes out of
I have the version of NuGet that comes with MVC3 (1.0), but when I
I have an MVC3 application, in which the user is sent an email, having
I am using Code First, Entities, MVC3 + Razor, we have forms which have
Firstly, I'm new to MVC - and trying out ASP.NET MVC3. I want to
So I've build this website in MVC3 in which I am using the default
I need a library for my ASP.NET MVC3 Application that can convert to and
Disclaimer : I'm still starting out with MVC3 (and OAuth in particular). I might
I'm installing ASP.NET MVC 3 Beta and this thing comes out : This product

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.