Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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
Jquery 使用Web Api和FromBody_Jquery_Asp.net Web Api_Frombodyattribute - Fatal编程技术网

Jquery 使用Web Api和FromBody

Jquery 使用Web Api和FromBody,jquery,asp.net-web-api,frombodyattribute,Jquery,Asp.net Web Api,Frombodyattribute,我的api有一个post方法,如下所示: // POST api/collections public HttpResponseMessage Post([FromBody]Collection model) { if (!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); using (var uow = new UnitO

我的api有一个post方法,如下所示:

// POST api/collections
public HttpResponseMessage Post([FromBody]Collection model)
{
    if (!ModelState.IsValid)
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);

    using (var uow = new UnitOfWork<SkipstoneContext>())
    {
        var service = new CollectionService(uow, User.Identity.GetUserId());

        service.Save(model);
        uow.SaveChanges();

        return Request.CreateResponse(HttpStatusCode.OK, model);
    }
}
当我这样做时,我得到了一个400错误请求响应。如果在api方法中设置断点,我会发现Name为空。 以下是我的收藏模式:

public partial class Collection
{
    public int Id { get; set; }
    public string CreatedById { get; set; }
    [Required] public string Name { get; set; }
    public string Description { get; set; }
    public System.DateTime DateCreated { get; set; }
    public string ModifiedById { get; set; }
    public Nullable<System.DateTime> DateModified { get; set; }

    public User CreatedBy { get; set; }
    public User ModifiedBy { get; set; }
    public ICollection<Asset> Assets { get; set; }
}
公共部分类集合
{
公共int Id{get;set;}
公共字符串CreatedById{get;set;}
[必需]公共字符串名称{get;set;}
公共字符串说明{get;set;}
public System.DateTime DateCreated{get;set;}
公共字符串ModifiedById{get;set;}
公共可为空的DateModified{get;set;}
公共用户CreatedBy{get;set;}
公共用户通过{get;set;}修改
公共ICollection

它告诉我需要用一个空键发送参数,但我不能用我的集合来发送参数

有人知道如何避开这个问题吗?或者是一个更好的方法

任何帮助都将不胜感激

/r3plica

尝试使用这样的方法,应该可以:-

            var myData = { 
                 'Id':  id, 
                 'Name':  $('#Name').val(), 
                 'Description': $('#Description').val() 
                };

             $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: url,
                    data: myData,
                    success: function (data) {
                        alert(data);
                    }
                });
            var myData = { 
                 'Id':  id, 
                 'Name':  $('#Name').val(), 
                 'Description': $('#Description').val() 
                };

             $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: url,
                    data: myData,
                    success: function (data) {
                        alert(data);
                    }
                });