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

4

我最近一直在使用新的ASP.NET 5系统和微软的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#类:

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


尝试删除JSON.stringify,并将纯对象作为数据传递:data: { model: _this.model, editing: true } - Domysee
还可以尝试在 editing 上添加 [FromBody] - Domysee
我已经尝试了这两种方法,但都没有成功。 - Ciel
有关这个问题的任何更新?今天我花了4个小时尝试各种组合来获取多个参数在.Net Core 1.1上。为什么我们不能像在MVC 5中那样顺畅地使用ajax和json进行发布,这是否有任何逻辑解释?我没有找到任何错误跟踪或逻辑解释为什么这不起作用。 - adopilot
5个回答

2

我有一个类似的问题。我通过使用单独的数据类来解决了它。

[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 } }
});

1
我可以确认这个有效;但是为什么?为什么MVC 5和6之间的行为不同?有没有办法解决它?乍一看,我认为问题是因为发送到控制器的JSON实际上有两个根级对象,而它无法映射它们 - 但这似乎很奇怪。 - Ciel
这并没有什么不同 - 这也是MVC 5的工作原理。我使用MVC 5准备了我的答案。 - tdragon

1

试试这个:

$.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 }
});

任何一个都应该可以工作。


很抱歉,这两个都不起作用。我继续得到“null”。 - Ciel

1
创建一个新类,其中包含 Profile Modelbool Editing 属性。然后,在你的 API 方法中使用它作为一个带有 [FromBody] 属性的参数。
[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 });

0

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);
}

你的类:

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

希望这能帮到你。


0
如果你使用此查询并删除 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) {

}

1
不,它没有。我只是收到了一个jQuery错误。 TypeError:无法读取未定义的属性“field” - Ciel
@Ciel 很有趣,但至少还有一个错误。我不确定是什么原因导致的,但如果你解决了它,它应该可以在 ASP.NET 方面工作。这正是我的应用程序所使用的方式。 - Domysee
@Ciel,错误出现在哪里?在$.ajax调用中吗? - Domysee
@Ciel 我不懂Angular,所以在那方面无法帮助你,抱歉。但我想问的问题是,为什么在你的请求版本中没有出现错误? - Domysee
2
混合使用jQuery和Angular(除了指令)是个坏主意,最终会给你带来很多麻烦。尽管如此,问题在于你的API方法 - POST应该只有一个带有[FromBody]属性的参数。 - tdragon
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接