Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.Net中使用jQuery发布时WebApi接收空参数_C#_.net_Asp.net Web Api - Fatal编程技术网

C# 在ASP.Net中使用jQuery发布时WebApi接收空参数

C# 在ASP.Net中使用jQuery发布时WebApi接收空参数,c#,.net,asp.net-web-api,C#,.net,Asp.net Web Api,因此,我尝试构建一个简单的API,它允许我创建客户,我在VS2012上使用ASP.NET Web API,我使用默认的项目配置。 以下是我所拥有的: HTML 模型 公共类客户:基本模型 { 私有字符串\u名称; 公共字符串名 { 获取{return\u name;} 设置{u name=value;} } 私人电话; 公用串电话 { 获取{return\u phone;} 设置{u phone=value;} } 私人字符串\u电子邮件; 公共字符串电子邮件 { 获取{return\u ema

因此,我尝试构建一个简单的API,它允许我创建客户,我在VS2012上使用ASP.NET Web API,我使用默认的项目配置。 以下是我所拥有的:

HTML

模型

公共类客户:基本模型
{
私有字符串\u名称;
公共字符串名
{
获取{return\u name;}
设置{u name=value;}
}
私人电话;
公用串电话
{
获取{return\u phone;}
设置{u phone=value;}
}
私人字符串\u电子邮件;
公共字符串电子邮件
{
获取{return\u email;}
设置{u email=value;}
}
私人区域(u区域),;
公共虚拟区域
{
获取{return\u region;}
设置{u region=value;}
}
私人产品清单;
公共虚拟列表产品
{
得到
{
如果(_products==null)
{
_产品=新列表();
}
退货产品;
}
设置{u乘积=值;}
}
私人名单(客户推荐);;
公共虚拟列表客户建议
{
得到
{
if(_customerPromotions==null)
{
_customerPromotions=新列表();
}
返回(u customer promotions);;
}
设置{u customerPromotions=value;}
}
私人国际?_regionId;
公共int?区域ID
{
得到
{
返回区域ID;
}
设置
{
_regionId=值;
}
}
}

发生的情况是,当我将表单提交到POST方法,但customer为null时,有人知道为什么会发生这种情况吗?

您在$.ajax函数中缺少数据

$.ajax({
  datatype:'json',
  data: yourformdata,
  ...
});

检查是否在控制台中发现任何错误。。另外,您似乎没有以json格式从服务器发送数据。我认为,由于我有一个表单,它将捕获表单值并传递给其他人。如果这不是假设,那么在这种情况下我将如何格式化数据?jQueryAjax不会自动捕获表单数据。我通常序列化表单并以$.ajax格式传递数据。HTML-
..
jQuery-
$.ajax({data:$(“#frm”).serialize()})
// POST api/customer
public Customer Post(Customer customer)
{
     CustomerService customerService = new CustomerService();
     customerService.CreateCustomer(customer);
     return customer;
}
public class Customer : BaseModel
{

    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _phone;

    public string Phone
    {
        get { return _phone; }
        set { _phone = value; }
    }

    private string _email;

    public string Email
    {
        get { return _email; }
        set { _email = value; }
    }

    private Region _region;

    public virtual Region Region
    {
        get { return _region; }
        set { _region = value; }
    }

    private List<Product> _products;

    public virtual List<Product> Products
    {
        get
        {
            if (_products == null)
            {
                _products = new List<Product>();
            }
            return _products;
        }
        set { _products = value; }
    }


    private List<CustomerPromotion> _customerPromotions;

    public virtual List<CustomerPromotion> CustomerPromotions
    {
        get
        {
            if (_customerPromotions == null)
            {
                _customerPromotions = new List<CustomerPromotion>();
            }
            return _customerPromotions;
        }
        set { _customerPromotions = value; }
    }

    private int? _regionId;
    public int? RegionId
    {
        get
        {
            return _regionId;
        }
        set
        {
            _regionId = value;
        }
    }

}
$.ajax({
  datatype:'json',
  data: yourformdata,
  ...
});