ASP.NET MVC3 JSON Model-binding with nested class

前端 未结 2 1307
闹比i
闹比i 2020-12-24 15:13

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
 {
         


        
相关标签:
2条回答
  • 2020-12-24 15:45

    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 讨论(0)
  • 2020-12-24 15:50

    I experienced the same issue, however in my case this was related to how the data was being transmitted from client side. Make sure the AJAX request is using the proper headers and formatting. For example:

        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify({
            MemberId : '123',
            UserName: '456',
            Parameters: [
                { Value : 'testing' },
                { Value : 'test2' }
            ]
        }),
    
    0 讨论(0)
提交回复
热议问题