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

标签 c# javascript ajax asp.net-mvc asp.net-mvc-4

是否有将多个对象发布到 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'
       }
   }
});

关于c# - MVC4 Controller 将多个 json 对象发布到 Controller ,ajax 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17483109/

相关文章:

c# - PropertyInfo.GetValue 返回枚举常量名称而不是值

c# - 构造函数参数枚举和魔术字符串

c# - 如何在 C# 中声明经典 COM 接口(interface) IBindCtx?

javascript - 如何使标题闪烁直到它与 jQuery 一起激活?

javascript - 如何从中心在图像上方画一条垂直线?

javascript - 如何使用 jquery 将 JavaScript 数组发布到 Laravel Controller

c# - XML 属性的选择性序列化

javascript - jQuery 从类名中选择一个元素

javascript - 处理从 AJAX 调用返回的数据

jquery - jQuery .ajax() 是否需要 CSRF token ?