Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery MVC 6-无法再将多个JSON参数传递给控制器_Jquery_Json_Asp.net Mvc_Asp.net Core Mvc - Fatal编程技术网

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

Jquery MVC 6-无法再将多个JSON参数传递给控制器,jquery,json,asp.net-mvc,asp.net-core-mvc,Jquery,Json,Asp.net Mvc,Asp.net Core Mvc,我最近一直在努力使用新的ASP.NET 5系统和Microsoft的ASP.NET MVC 6,但我发现一些非常关键的问题阻碍了我的发展;最值得注意的是,我似乎无法再通过控制器方法传递多个JSON参数 例如,我有以下控制器方法 [HttpPost] [Route("edit/profile")] public void Attribute([FromBody]Models.Profile model, bool editing) { // ... // model always

我最近一直在努力使用新的
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…})
部分,只传入一个参数,它就会像预期的那样通过
数据:JSON.stringify(_this.model)

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

它对应于这个
C#

namespace Models {
    public class Profile {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Text { get; set; }
    }
}
我一辈子都想不出来。这在
MVC5
上运行得很好,但由于升级,它完全失效了

我也在使用jQuery 2.1.4,试试这个:

$.ajax({
    url: '/edit/profile',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: { model: JSON.stringify(_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) {

}

使用
配置文件模型
bool编辑
属性创建一个新类。然后,在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 });

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

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

json文件中的json objct:

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

我希望这会有所帮助。

尝试删除
JSON。字符串化
并使用普通对象作为数据:
数据:{model:_this.model,editing:true}
也尝试将
[FromBody]
添加到
编辑
中,我已经尝试了这两种方法,但都无效。对此进行任何更新,今天,我花了4个小时尝试在.NETCore1.1上使用任意组合来获取多个参数。我们为什么不能像在MVC5中那样顺利地发布ajax和json呢?。我没有任何错误跟踪或逻辑解释为什么这不起作用。对不起,不幸的是,这两项都不起作用。我继续得到
null
。不,它没有。我刚刚得到一个jQuery错误<代码>类型错误:无法读取未定义的属性“字段”
@Ciel interest,但至少存在另一个错误。我不确定是什么原因造成的,但如果您解决了它,它应该在ASP.NET端工作。这正是它在我的应用程序中的工作方式。@Ciel您确定
\u此
不是
未定义的
?@Ciel错误发生在哪里?在$.ajax调用中,混合使用jQuery和Angular(除了指令之外)是个坏主意,最终会让您非常头疼。尽管如此,问题在于您的API方法-POST应该只有一个带有
[FromBody]
属性的参数;但是为什么呢?为什么MVC5和MVC6的这种行为不同?有没有办法解决这个问题?乍一看,我认为这个问题是因为进入控制器的
JSON
实际上有两个根级别的对象,它无法映射它们-但这看起来很奇怪。这没什么不同-这也是MVC 5中的工作方式。我用MVC5准备了我的答案。
[HttpPost]
public ActionResult AddPeople(Teste PeopleJson)
{
   return Json(PeopleJson);
}
   public class Teste
   {
      public string Name { get; set; }
   }