的ASP.NET MVC控制器不绑定的所有型号属性时发布从JQuery的阿贾克斯

问题描述:

在调试模式结合的基本属性,但字段列表成为空 但我想利用这个网络API控制器模型网络API完美结合。 同一代码片段适用于Web API控制器。的ASP.NET MVC控制器不绑定的所有型号属性时发布从JQuery的阿贾克斯

这里是我的视图模型

public class FieldGroup 
    { 

     public int FieldGroupID { get; set; } 
     public string GroupName { get; set; } 
     public string Type { get; set; } 
     public ICollection<Field> Fields { get; set; } 

    } 
public class Field 
    { [Key] 
     public int SourceFieldID { get; set; } 
     public string InternalID { get; set; } 
     public string Name { get; set; } 
     public string Value { get; set; } 
    } 

这里是我的MVC控制器

 [HttpPost] 
     public ActionResult Create(FieldGroup demo) 
     { 

     //do nothing 

      return Json(demo); 
     } 

这里是我的JQuery的Ajax方法

var postData = { 
 
    GroupName: "demogroup", 
 
    Fields: [{ 
 
    Name: "field1", 
 
    Value: "value1", 
 
    InternalID: "a1" 
 
    }, { 
 
    Name: "field2", 
 
    Value: "value2", 
 
    InternalID: "a2" 
 
    }, ], 
 
    Type: "sourcefield" 
 
}; 
 

 
$.ajax({ 
 
    type: "POST", 
 
    url: '/Home/Create', 
 
    contentType: "application/json", 
 
    dataType: "json", 
 
    data: JSON.stringify(postData || {}), 
 
    success: function(result) { 
 
    console.log(result); 
 
    } 
 
});

+0

这个片段是为我工作很好,我已经试过只用MVC控制器。 MVC和JQuery使用哪个版本? –

+0

我使用MVC 5.1.0但在的WebAPI控制器它 – sathish

+0

感谢的人,现在它工作正常工作对我。现在我再次尝试它的作品,可能是我只是错过了一些参数前面感谢..检查 – sathish

这段代码是工作的罚款对我来说,我已经试过只MVC控制器。您可以更改版本并尝试。对我来说,我试图与MVC-5jQuery的1.10.2

+0

感谢的jQuery的版本导致问题......改变它工作正常版本....... – sathish

+0

但荫得到内部服务器错误500后,我在我的控制器添加ValidateAntiForgeryToken后。而且我还将请求令牌添加到了我的ajax请求中 – sathish