【问题标题】:ASP.Net MVC Model Binding to JSON in EditorForASP.Net MVC 模型在 EditorFor 中绑定到 JSON
【发布时间】:2013-04-29 17:51:02
【问题描述】:

我希望能够通过隐藏文本框中的 JSON 将信息从我的视图模型传递到我的控制器。我正在使用 Google 地图 API 中的多边形。当用户编辑多边形时,我通过 javascript 将顶点存储在隐藏的输入中。

var p = mypolygon.getPath().getArray();
var s = '';
for (var i = 0; i < p.length; i++)
    s += ((i > 0) ? ', ' : '') + '{ lat: ' + p[i].lat() + ', lng: ' + p[i].lng() + ' }';
$('#@Html.IdFor(m => m.GeofencePoints)').val('[' + s + ']');

结果如下:

  <input id="GeofencePoints" name="GeofencePoints" type="hidden" value="[{ lat: 38.221276965853264, lng: -97.6892964859955 }, { lat: 38.21294239796929, lng: -97.68770861825868 }, { lat: 38.2122680083775, lng: -97.67782997884831 }, { lat: 38.220434074436966, lng: -97.67787289419255 }]">

我希望将以下视图模型绑定到视图:

public class MyMapViewModel
{
    public GoogleMapPoint[] GeofencePoints {get;set;}
    public string OtherProperty {get;set;}
}

public class GoogleMapPoint
{
    public double lat {get;set;}
    public double lng {get;set;}
}

这与我看到的示例略有不同,因为我只想将我的一个属性发布为 Json。谁能指出我正确的方向?我知道我可以将它作为字符串传递并自行序列化/反序列化。但是,我希望有一个优雅的客户模型绑定器解决方案。

更新

根据我发现的这篇文章,我想出了一个通用的解决方案: http://mkramar.blogspot.com/2011/05/mvc-complex-model-postback-bind-field.html

public class JsonBindableAttribute : Attribute
{
}

public class MyModelBinder : DefaultModelBinder
{
    protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
    {
        if (propertyDescriptor.Attributes.OfType<Attribute>().Any(x => (x is JsonBindableAttribute)))
        {
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
            return JsonConvert.DeserializeObject(value, propertyDescriptor.PropertyType);
        }

        return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
    }
}

在我的模型中:

[JsonBindable]
[UIHint("GoogleMapPoints")]
public GoogleMapPoint[] GeofencePoints { get; set; }

然后在 global.asax Application_Start()

ModelBinders.Binders.DefaultBinder = new MyModelBinder();

不幸的是,这只能让我到目前为止。这将表单值绑定到我的类就好了。但是,它不能解决将属性呈现为 Json 的问题。如您所见,我创建了一个自定义编辑器 GoogleMapPoints.cshtml,基本上我必须为每个我将拥有的 jsonbindable 类重新创建它。

@model IEnumerable<GoogleMapPoint>
@Html.Hidden("", Newtonsoft.Json.JsonConvert.SerializeObject(Model))

有没有人知道有一种通用自定义编辑器的方法,它会关注属性而不是类型,以便使用我的 JsonBindable 属性着色的属性的 EditorFor 始终呈现为隐藏字段中的 Json,而不管类型/类如何?

【问题讨论】:

    标签: json asp.net-mvc-3 custom-model-binder


    【解决方案1】:

    您可以为该特定模型创建模型绑定器。这将通过为保存地图点的属性添加一些特定逻辑来扩展默认绑定器,从请求参数中反序列化 json。

    [ModelBinder(typeof(MyMapModelBinder))]
    public class MyMapViewModel
    {
        public List<GoogleMapPoint> GeofencePoints { get; set; }
        public string OtherProperty { get; set; }
    }
    
    public class GoogleMapPoint
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    
    public class MyMapModelBinder : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Name == "GeofencePoints")
            {
                var model = bindingContext.Model as MyMapViewModel;
                if (model != null)
                {
                    var value = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
                    var jsonMapPoints = value.AttemptedValue;
    
                    if (String.IsNullOrEmpty(jsonMapPoints))                    
                        return ;                    
    
                    MyMapViewModel mapModel = model as MyMapViewModel;
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    mapModel.GeofencePoints = (List<GoogleMapPoint>)serializer.Deserialize(jsonMapPoints, typeof(List<GoogleMapPoint>));
                    return;
                }
            }
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多