【发布时间】:2012-01-16 21:39:33
【问题描述】:
我的 json 或 ASP.NET MVC 有问题,我正在使用 ASP.NET MVC,这是我从客户端发送的内容。
注意 在 Chrome 中调试后,我解释说这是在 javascript 中传递的内容,我没有手动将 State 设置为 null,因为它是来自其他地方的结果为 null。这又一次不在我的控制范围内,因为它来自数据库。
在调试时,State 显示为 null,而不是“null”,但在 MVC 中调试时,它显示的是“null”而不是 null。
$.ajax(
'/Client/Post',
{
method: 'POST',
data: {
Country: 'US',
// this is null because it is coming from somewhere else as null
State: null
}
});
我的 ASP.NET MVC 处理程序接收...
public ActionResult Post(Client model){
if(model.State == "null")
{
/// this is true... !!!!
}
if(model.State == null )
{
// :( this should be true...
}
}
是 ASP.NET MVC 还是 jQuery 的问题?
那么是 jQuery 将 null 作为“null”发送还是 MVC 将 null 设置为“null”?
解决方案
我不得不递归地创建新的对象层次结构(克隆对象)并将其发送到 jQuery,因为 jQuery 以 Form Encoded 形式发送数据,其中无法表示 null,但理想情况下 jQuery 不应该在全部。
【问题讨论】:
-
如果您必须发布数据而不是作为 json。见链接stackoverflow.com/questions/8442283/…
-
我同时使用 IE9、Chrome、Firefox,结果都一样。
标签: javascript jquery ajax asp.net-mvc