简体   繁体   English

MVC,Jquery和Ajax-控制器中具有空参数的JSON

[英]MVC, Jquery and Ajax - JSON with null parameters in controller

I have a problem with passing object with properties into controller. 我将带有属性的对象传递到控制器时遇到问题。
Both are always null. 两者始终为null。
In console.log passed data looks fine. 在console.log中传递的数据看起来不错。 Where I made a mistake? 我在哪里弄错了?
I looked into other solutions and nothing pushed me to the correct way... 我研究了其他解决方案,但没有任何事情促使我采取正确的方法。

C#, MVC5, Jquery 2.1.1 C#,MVC5,jQuery 2.1.1

Controller: 控制器:

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

Action: 行动:

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

And View: 并查看:

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

Your view model Properties must be Public ! 您的视图模型Properties必须是Public

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

I suggest the first word of the Properties be uppercase. 我建议“ Properties的第一个单词为大写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM