MVC API控制器的复杂的对象没有得到来自jQuery的Ajax调用填充 [英] MVC API controller's complex object not getting populated from jquery ajax call

查看:127
本文介绍了MVC API控制器的复杂的对象没有得到来自jQuery的Ajax调用填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用API POST控制器。该控制器被调用,但复杂的对象进来空。我已经跑提琴手和对象甚至是通过填充来了那里。我在做什么错了?

我的C#对象

 公共类RegisterUser
{
    公众的Guid PERSONID {搞定;组; }
    公共字符串电子邮件{获得;组; }
    公共字符串商务{搞定;组; }
    公共字符串EmployeeNumber {搞定;组; }
    公共字符串用户名{获得;组; }
}

API邮政控制器

 公开的Htt presponseMessage帖​​子(RegisterUser用户)
{
   //这是问题的所在。在用户的一切都是空
   //即使我能看到它的到来通过的提琴手。
}

的Javascript code

 函数用户(PERSONID,用户名,电子邮件,商务,employeeNumber){
   this.PersonId = PERSONID;
   this.Email =电子邮件;
   this.Business =业务;
   this.EmployeeNumber = employeeNumber;
   this.UserName =用户名;
}功能RegisterUser(URL){
   VAR createdUser =新用户(b3fd25ba-49e8-4247-9f23-a6bb90a62691,用户名,电子邮件,商界,56465);
   $就(URL,{
       数据:JSON.stringify({用户:createdUser})
       类型:后,
       的contentType:应用/ JSON
  });
}

网络API路由配置

 公共静态类WebApiConfig
{
    公共静态无效的注册(HttpConfiguration配置)
    {
        config.Routes.MapHttpRoute(
            名称:RegisterApi
            routeTemplate:API / {控制器} / {}用户
        );
    }
}


解决方案

createdUser 已经包含了正确的格式通过Web.Api需要也没有必要来包装数据它其迷惑模型粘合剂的用户属性里面。

只要写数据:JSON.stringify(createdUser),它应该很好地工作:

 函数RegisterUser(URL){
   VAR createdUser =新用户(b3fd25ba-49e8-4247-9f23-a6bb90a62691,用户名,电子邮件,商界,56465);
   $就(URL,{
       数据:JSON.stringify(createdUser)
       类型:后,
       的contentType:应用/ JSON
  });
}

模型结合的原理很简单,直到你在你的JS和C#对象的匹配属性名称和对象结构,它应该工作正常。

I am trying to call a POST API controller. The controller gets called but the complex object comes in empty. I have ran Fiddler and the object is even coming through populated there. What am I doing wrong?

My C# object

public class RegisterUser
{
    public Guid PersonId { get; set; }
    public string Email { get; set; }
    public string Business { get; set; }
    public string EmployeeNumber { get; set; }
    public string UserName { get; set; }
}

API Post Controller

public HttpResponseMessage Post(RegisterUser user)
{
   //This is where the problem is. Everything in user is null 
   //even though I can see it coming through on Fiddler.
}

Javascript code

function User(personId, userName, email, business, employeeNumber) {
   this.PersonId = personId;
   this.Email = email;
   this.Business = business;
   this.EmployeeNumber = employeeNumber;
   this.UserName = userName;
}

function RegisterUser(url) {
   var createdUser = new User("b3fd25ba-49e8-4247-9f23-a6bb90a62691", "username", "email", "business", "56465");
   $.ajax(url, {
       data: JSON.stringify({ user: createdUser }),
       type: "post",
       contentType: "application/json"
  });
}

Web API Route Config

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "RegisterApi",
            routeTemplate: "api/{controller}/{user}"
        );
    }
}

解决方案

createdUser already contains the data needed by Web.Api in the correct format there is no need to wrap it inside an user property which confuses the model binder.

Just write data: JSON.stringify(createdUser) and it should work fine:

function RegisterUser(url) {
   var createdUser = new User("b3fd25ba-49e8-4247-9f23-a6bb90a62691", "username", "email", "business", "56465");
   $.ajax(url, {
       data: JSON.stringify(createdUser),
       type: "post",
       contentType: "application/json"
  });
}

The principles of the model binding are simple until you have matching property names and object structure in your JS and C# objects and it should work fine.

这篇关于MVC API控制器的复杂的对象没有得到来自jQuery的Ajax调用填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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