淘汰赛和MVC3:发布JSON行动,序列化两次?无法转换为C#对象? [英] Knockout and MVC3: Posting JSON to action, serializing twice? Can't convert to C# objects?

查看:189
本文介绍了淘汰赛和MVC3:发布JSON行动,序列化两次?无法转换为C#对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个被通过保存方法发布了淘汰赛型号:

I've got a Knockout Model that gets posted via a save method:

 self.save = function(form) {
        ko.utils.postJson($("form")[0], self);
    };

我检查,以确保所有的数据正确张贴(它是)要求:

I check the request to make sure all the data is properly being posted (it is):

然而,当我到达我的行动:

However, when I get to my action:

 [HttpPost]
 public ActionResult Create(EquipmentCreateModel equipmentCreateModel)
 {
    /stuff here
 }

大厦code和客房包含转义引号和标识是完全不为空,但有计数0:

BuildingCode and Room contain escaped quotes, and identifiers is totally not null but has a count of 0:

和我的ModelState是无效的,有一个错误,在标识符其中有企图值属性:

And my ModelState is not valid, there is one error, for the Identifiers property which has an attempted value of :

和异常信息是:

参数转换,从类型'System.String'输入'System.Collections.Generic.KeyValuePair`2 [的System.Guid,mscorlib程序,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089],[ System.String,mscorlib程序,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089]]'失败,因为没有类型转换器可以将这些类型之间的转换。

"The parameter conversion from type 'System.String' to type 'System.Collections.Generic.KeyValuePair`2[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' failed because no type converter can convert between these types."

我的模型:

public class EquipmentCreateModel
{
//used to populate form drop downs
public ICollection<Building> Buildings { get; set; }
public ICollection<IdentifierType> IdentifierTypes { get; set; }

[Required]
[Display(Name = "Building")]
public string BuildingCode { get; set; }

[Required]
public string Room { get; set; }

[Required]
[Range(1, 100, ErrorMessage = "You must add at least one identifier.")]
public int IdentifiersCount { get; set; } //used as a hidden field to validate the list
public string IdentifierValue { get; set; } //used only for knockout viewmodel binding

public IDictionary<Guid, string> Identifiers { get; set; }
}

现在开始我还以为是敲一个问题,但后来我发现数据没有被张贴在请求正确。我固定的,仍然有同样的问题。我以为MVC3自动转换Json的呢?为什么我的简单的属性出现在转义引号,为什么不能我的身份集合正确地从发布的数据填充?

Now first I thought it was a problem with knockout, but then I found out the data wasn't being posted in the request correctly. I fixed that and still had the same problem. I thought MVC3 automatically converts Json now? Why are my simple properties appearing in escaped quotes and why can't my identities collection properly populate from the posted data?

推荐答案

试试这个:

 [HttpPost]
 public ActionResult Create([FromJson] EquipmentCreateModel equipmentCreateModel)
 {
    //stuff here
 }

其中FromJson是:

where FromJson is:

   public class FromJsonAttribute : CustomModelBinderAttribute
    {
      private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer();
      public override IModelBinder GetBinder()
      {
         return new JsonModelBinder();
      }

      private class JsonModelBinder : IModelBinder
      {
         public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
         {
           var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
           if (string.IsNullOrEmpty(stringified))
                return null;
           return serializer.Deserialize(stringified, bindingContext.ModelType);
          }
     }
 }

这是摘自:
http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/
您应检查意见,因为有对FromJsonAttribute做一些修改。

This is taken from: http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/ you should check the comments to as there are some modification for the FromJsonAttribute.

这篇关于淘汰赛和MVC3:发布JSON行动,序列化两次?无法转换为C#对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆