Skip to main content
 首页 » 编程设计

c#之MVC4 Controller 将多个 json 对象发布到 Controller ,ajax 发布

2024年01月28日36dyllove98

是否有将多个对象发布到 Controller 的示例。 ajax 帖子的数据必须是什么样子?

[HttpPost] 
public string Register(UserLogin userLogin, Contact contact) 
{ 
} 

用户登录

public class UserLogin  
{ 
   public string Username { get; set; } 
   public string Password { get; set; } 
} 

联系方式

public class Contact 
{ 
   public string Firstname { get; set; } 
   public string Lastname { get; set; } 
} 

AJAX ?

$.ajax({ 
   type: "POST", 
   url: "SomeUrl" 
   dataType: "json", 
   contentType: "application/json; charset=utf-8", 
   data: ? }); 

请您参考如下方法:

试试这个

$.ajax({ 
   type: "POST", 
   url: "SomeUrl" 
   dataType: "json", 
   contentType: "application/json; charset=utf-8", 
   data: { 
       'userLogin' : { 
           'Username' : 'Username', 
           'Password' : 'Password' 
       }, 
       'contact' : { 
           'Firstname' : 'Firstname', 
           'Lastname' : 'Lastname' 
       } 
   } 
});