我正在尝试将一个对象从 jquery 发布到 MVC 控制器。该对象已成功传递,但所有属性均为空(或布尔为假)。
fwiw,如果我 JSON.stringify myObect 那么它根本不通过,控制器中的 theObect 为空。
我正在使用 MVC4、.net 4.5、jQuery 1.9.1
任何帮助表示赞赏。
jQuery函数
var myObject =
{
Property1: true,
Proerty2: true
};
$.ajax({
type: 'POST',
url: '/myController/StartProcess/',
data: { theObject: myObject }
});
控制器
private async void StartProcess(myObject theObject)
{
// theObect can be seen successfully but property1 and property2 are false
// if I change them to strings they are null
...
}
班级
public class myObject
{
public bool Property1 { get; set; }
public bool Property2 { get; set; }
}
编辑:
解决方案是:
$.ajax({
type: 'POST',
url: '/myController/StartProcess/',
data: myObject
});
如果任何人都可以阐明为什么这是有效的,而没有其他任何作用,将不胜感激。这不是一个很好的解决方案,因为我必须将所有参数放在 myObject 中,我不能使用这种技术传递任何其他参数。也很好奇为什么我在网上找到的所有信息,包括官方教程,都说使用 data: JSON.Strinify(myObect) 但对我来说这会导致 myObject 的所有属性为空(或假)。
仍然感谢咆哮,至少我可以超越这一点。