简体   繁体   English

模型活页夹无法使用JSON

[英]Model Binder not working with JSON

Hello am trying to do a WebAPI + Windows Phone APP to interact. 您好,我正在尝试做一个WebAPI + Windows Phone APP进行交互。 Project is Web API MVC4 .NET 4.5 I have the following issues: Model binding is not working or at last not populating internal fields. 项目是Web API MVC4 .NET 4.5我遇到以下问题:模型绑定不起作用,或者最后没有填充内部字段。

If you try to see the variable "toRegister" from register its always null and everything null. 如果尝试从注册中查看变量“ toRegister”,则该变量始终为null,所有内容均为null。 And it should populate right?. 它应该填充对吗?

Note: User Object is in both cases the same class. 注意:在两种情况下,用户对象都是同一类。

Server Code. 服务器代码。

public SessionAPI register(User toRegister)
{  //SomeCodeHere }

Client Code. 客户代码。 (Windows Phone 8) (Windows Phone 8)

User toPost = new User();
toPost.Name = "test";
toPost.Email = "email@email.com";
toPost.PhoneNumber = "13243";
toPost.Password = "secret";

WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.Headers["Content-Type"] = "application/json";
webClient.UploadStringAsync(new Uri(Destination), "POST" ,JsonConvert.SerializeObject(toPost));
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadRegistrationCompleted);    

Thank you for your time. 感谢您的时间。

You are doing a POST in your code so your API controller should have a method called Post: 您正在代码中执行POST ,因此您的API控制器应具有一个称为Post的方法:

public SessionAPI Post(User toRegister)
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM