.net c#将字符串绑定到控制器方法中的类

Bind string to class in controller method
2020-11-24
  •  译文(汉语)
  •  原文(英语)

我想像这样绑定JSON的提交

{
 "id": 1,
 "name": "bob",
 "phone": "(425) 882-8080"
}

至...

class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public PhoneNumber Phone { get; set; }
}

其中PhoneNumber类能够绑定到JSON中的电话字符串.这个想法是使用json.net自定义转换器,例如:

class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    [JsonConverter(typeof(PhoneNumberCoverter))]
    public PhoneNumber Phone { get; set; }
}

问题在于MVC甚至没有尝试使用该ReadJson方法.Model.Phone ==空.

我已经尝试了几件事.希望如果我在PhoneNumber类的字符串之间有隐式的操作性覆盖,它可能会自动执行.不.

针对这种情况自定义模型绑定的正确方法是什么?

速聊1:
这里答案可能与您的问题有关
解决过程1

我认为您希望当您的操作方法像

public ActionResult Post(Model myModel)
{
  // you expect that myModel should has used JSonConverter and provide you data.
}

上面的事情不会按您预期的那样工作,并且此JsonConvertor属性的原因是特定的Json.net而不是MVC属性,因此DefaultModelBinder不会考虑该属性来绑定您的数据.

可能的正确而简单的操作方式(但不是强类型)

   public ActionResult Post(string jsonData)
   {
       // Now here use Json.net JsonConvert to deserialize string to object of Type Model.
   }

另一个解决方案是构建您自己的自定义modelbinder并在其中使用Json.net.

速聊1:
我从未处理过模型绑定器.如果是这样或者只是接收一个字符串,我想我会给Modelbinder一个机会.

I would like to bind submission of JSON like this

{
 "id": 1,
 "name": "bob",
 "phone": "(425) 882-8080"
}

to...

class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public PhoneNumber Phone { get; set; }
}

where the PhoneNumber class is able to bind to the phone string in the JSON. The idea was the use a json.net custom converter like:

class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    [JsonConverter(typeof(PhoneNumberCoverter))]
    public PhoneNumber Phone { get; set; }
}

The problem is that MVC is not even trying to use the ReadJson method. Model.Phone == null.

I have tried a few things. Hoping that if I had implicit operative overrides to and from string for the PhoneNumber class, it may just do it automatically. Nope.

What is the correct way to customize model binding for this scenario?

Talk1:
The answers here might be related to your issue
Solutions1

I think you expect that when your action method like

public ActionResult Post(Model myModel)
{
  // you expect that myModel should has used JSonConverter and provide you data.
}

Above thing will not work as you expected and reason for this JsonConvertor attribute is specific Json.net and not MVC attribute so DefaultModelBinder will not consider that attribute to bind your data.

Possible correct and simple way to do (But not strongly type)

   public ActionResult Post(string jsonData)
   {
       // Now here use Json.net JsonConvert to deserialize string to object of Type Model.
   }

Another solution to is to build your own custom modelbinder and use Json.net inside that.

Talk1:
I have never dealt with model binder. If it is that or just receive a string, I think I will give modelbinder a go.
转载于:https://stackoverflow.com/questions/28121834/bind-string-to-class-in-controller-method

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入