MVC 6 - 无法再将多个 JSON 参数传递给控制器

MVC 6 - cannot pass multiple JSON parameters to a controller anymore

我最近一直在努力使用新的 ASP.NET 5 系统和 Microsoft 的 ASP.NET MVC 6,但我发现一些非常关键的问题阻碍了我继续前进;最值得注意的是,我似乎无法再通过控制器方法传递多个 JSON 参数。

比如我有如下控制器方法;

[HttpPost]
[Route("edit/profile")]
public void Attribute([FromBody]Models.Profile model, bool editing) {
    // ...
    // model always comes in null
    // editing always comes in default (false)
}

我正在尝试使用 $.ajax.

将数据传递给此控制器
$.ajax({
    url: '/edit/profile',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: JSON.stringify({ model: _this.model, editing: true })
});

但无论我做什么,参数总是 null 进入控制器。我尝试了各种方法,如果我忽略 ({ model : _this.model ... }) 部分并只传入一个参数,它会按预期通过 data: JSON.stringify(_this.model)

模型是这样的; (显然不是最终模型,只是我处理这个问题时的一个假人)

_this.model = {
    Id: null,
    Name: null
    Text: null
};

而对应的是这个C#class;

namespace Models {
    public class Profile {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Text { get; set; }
    }
}

我这辈子都想不通。这在 MVC 5 上运行良好,但自从升级后它就完全失效了。

我也在用jQuery 2.1.4

试试这个:

$.ajax({
    url: '/edit/profile',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: { model: JSON.stringify(_this.model), editing: true }
});

$.ajax({
    url: '/edit/profile',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: { model: _this.model, editing: true }
});

两者都应该有效。

如果您使用此查询并删除 FromBody 属性,它应该可以工作。

$.ajax({
    url: '/edit/profile',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: { model: _this.model, editing: true }
});


[HttpPost]
[Route("edit/profile")]
public void Attribute(Models.Profile model, bool editing) {

}

使用 Profile Modelbool Editing 属性创建一个新的 class。然后,将其用作具有 [FromBody] 属性的 API 方法中的参数。

[Route("api/profile/edit")]
[HttpPost]
public void Post([FromBody] ProfileViewModel content)
{

}

public class ProfileViewModel
{
    public Profile Profile { get; set; }

    public bool Editing { get; set; }
}

public class Profile
{
    public string Name { get; set; }
}

$.ajax({
    url: '/api/profile/edit',
    method: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: JSON.stringify({ profile: { name: 'Test' }, editing: true })
});

在Angular(你使用它,正如你在评论中提到的),你可以使用:

$http.post('api/profile/edit', { profile: { name: 'Test' }, editing: true });

我也有类似的问题。我已经通过对数据使用单独的 class 解决了这个问题。

[HttpPost]
[Route("edit/profile")]
public void Attribute([FromBody] ProfileData data)
{
    var model = data.Model;
    var editing = data.Editing;
}

public class ProfileData
{
    public Models.Profile Model { get; set; }
    public bool Editing { get; set; }
}

$.ajax({
    url: '/edit/profile',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: { data: { Model: _this.model, Editing: true } }
});

js文件中的json对象:

var PeopleJson = {
        Name: "Volfgam"
}

发送控制器使用 $.post() 而不是 $.ajax:

$.post("/api/people", PeopleJson)
        .done(function (response) {
            that.IsLoading(false);
            alert(response);
        })
        .fail(function (error) {
            that.IsLoading(false);
            alert(error.statusText);
        });

你的控制器,很简单:

[HttpPost]
public ActionResult AddPeople(Teste PeopleJson)
{
   return Json(PeopleJson);
}

你的class:

   public class Teste
   {
      public string Name { get; set; }
   }

希望对您有所帮助。