【问题标题】:MVC, Jquery and Ajax - JSON with null parameters in controllerMVC、Jquery 和 Ajax - 控制器中带有空参数的 JSON
【发布时间】:2018-06-06 08:16:17
【问题描述】:

我在将具有属性的对象传递给控制器​​时遇到问题。
两者始终为空。
在 console.log 中传递的数据看起来不错。我哪里做错了?
我研究了其他解决方案,但没有什么能把我推向正确的方向......

C#、MVC5、Jquery 2.1.1

控制器:

public class Position  
{  
string intnew { get; set; }  
string intlast { get; set; }  
}

行动:

 [HttpPost]
    public ActionResult change(Position position)
    {
    return Json(new { result = "" });
    }

并查看:

var pos = new Object;
pos.intnew = '22';
pos.intlast = '11';

$.ajax({
url: '/szkolenia/change',
contentType: 'application/json; charset=utf-8',
datatype: 'json',
data: JSON.stringify({ position: pos }),
type: "POST",
success: function (_infoLogs) {
console.log(JSON.stringify({ position: pos }));
}
});

【问题讨论】:

  • 因为你的属性不是public
  • 耶稣....非常感谢!!!!
  • Spehen Muecke 所说的 + [FromBody] 位置,我认为
  • @WhoMightThisOneBe, [FromBody] 是 web-api,而不是 mvc
  • [FromBody] 仅适用于我所知道的 API。

标签: c# json ajax asp.net-mvc


【解决方案1】:

您的视图模型Properties 必须是Public

public class Position  
{  
  public string intnew { get; set; }  
  public string intlast { get; set; }  
}

我建议Properties 的第一个单词是大写的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2014-03-01
    相关资源
    最近更新 更多