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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:55:17+00:00 2026-05-23T11:55:17+00:00

In MVC3, is it possible to automatically bind javascript objects to models if the

  • 0

In MVC3, is it possible to automatically bind javascript objects to models if the model has nested objects? My model looks like this:

 public class Tweet
 {
    public Tweet()
    {
         Coordinates = new Geo();
    }
    public string Id { get; set; }
    public string User { get; set; }
    public DateTime Created { get; set; }
    public string Text { get; set; }
    public Geo Coordinates { get; set; } 

}

public class Geo {

    public Geo(){}

    public Geo(double? lat, double? lng)
    {
        this.Latitude = lat;
        this.Longitude = lng;
    }

    public double? Latitude { get; set; }
    public double? Longitude { get; set; }

    public bool HasValue
    {
        get
        {
            return (Latitude != null || Longitude != null);
        }
    }
}

When I post the following JSON to my controller everything except “Coordinates” binds successfully:

{"Text":"test","Id":"testid","User":"testuser","Created":"","Coordinates":{"Latitude":57.69679752892457,"Longitude":11.982091465576104}}

This is what my controller action looks like:

    [HttpPost]
    public JsonResult ReTweet(Tweet tweet)
    {
        //do some stuff
    }

Am I missing something here or does the new auto-binding feature only support primitive objects?

  • 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-23T11:55:18+00:00Added an answer on May 23, 2026 at 11:55 am

    Yes, you can bind complex json objects with ASP.NET MVC3.

    Phil Haack wrote about it recently.
    You’ve got a problem with your Geo class here.
    Don’t use nullable properties:

    public class Geo
    {
    
        public Geo() { }
    
        public Geo(double lat, double lng)
        {
            this.Latitude = lat;
            this.Longitude = lng;
        }
    
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    
        public bool HasValue
        {
            get
            {
                return (Latitude != null || Longitude != null);
            }
        }
    }
    

    This is the javascript code I’ve use to test it:

    var jsonData = { "Text": "test", "Id": "testid", "User": "testuser", "Created": "", "Coordinates": { "Latitude": 57.69679752892457, "Longitude": 11.982091465576104} };
    var tweet = JSON.stringify(jsonData);
    $.ajax({
        type: 'POST',
        url: 'Home/Index',
        data: tweet,
        success: function () {
            alert("Ok");
        },
        dataType: 'json',
        contentType: 'application/json; charset=utf-8'
    });
    

    UPDATE

    I’ve tried to do some experiments with model binders and I came out with this solutions which seems to work properly with nullable types.

    I’ve created a custom model binder:

    using System;
    using System.Web.Mvc;
    using System.IO;
    using System.Web.Script.Serialization;
    
    public class TweetModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var contentType = controllerContext.HttpContext.Request.ContentType;
            if (!contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
                return (null);
    
            string bodyText;
    
            using (var stream = controllerContext.HttpContext.Request.InputStream)
            {
                stream.Seek(0, SeekOrigin.Begin);
                using (var reader = new StreamReader(stream))
                    bodyText = reader.ReadToEnd();
            }
    
            if (string.IsNullOrEmpty(bodyText)) return (null);
    
            var tweet = new JavaScriptSerializer().Deserialize<Models.Tweet>(bodyText);
    
            return (tweet);
        }
    }
    

    and I’ve registered it for all types tweet:

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
    
            ModelBinders.Binders.Add(typeof(Models.Tweet), new TweetModelBinder());
    
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more like a conceptual question. When to use Model Binding (in ASP.NET
Is it possible to create an intellisense-like link to a class / class property
Is possible to integrate orchard to existing Asp.Net MVC3 site like separate area? Like
I have a simple MVC3 Controller Action like this [HttpGet] [OutputCache(Duration = 1200,Location=System.Web.UI.OutputCacheLocation.Server)] public
How do I instantiate the asp.net System.Web.UI.Page object in MVC3? Is this even possible?
I use Spring MVC 3.0 and JSP. I have an object: public class ObjectWrapper
Possible Duplicate: Session variable is lost on RedirectToAction in IE I'm using MVC3 and
I am working on a .Net MVC3 application. I have a couple different models
I've got a question about layout pages in MVC3: Is it possible to use
Possible Duplicate: Schedule Controls for ASP.Net MVC I have MVC3 in which I want

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.