[英]Model's property of type List<int> instantiating from POST despite value passed being null
I'm using ajax
to perform a post to an action with this declaration: 我正在使用
ajax
对该声明执行操作:
public ActionResult MonitorReport(FilterCaseViewModel model)
Amongst other things the FilterCaseViewModel
has this property: 除其他外,
FilterCaseViewModel
具有此属性:
public class FilterCaseViewModel
{
public List<int> Status { get; set; }
}
I'm using JSON
for the content type, The JSON
object starts like this (I've omitted the rest of it as it's quite long). 我正在使用
JSON
作为内容类型, JSON
对象以这样的方式启动(因为它很长,所以我省略了其余部分)。
{ model: {"TextSearch":null,"Generic1":null,"Generic2":null,"Status":null,"Team":null,"Client":null
Looking at the request form I'm getting an empty string interpreted for Status
查看请求表,我得到一个解释为
Status
的空字符串
Despite this however the Status
property has been instantiated and a value of 0
has been added to the list. 尽管如此,
Status
属性仍已实例化,并且将值0
添加到列表中。
I would expect the Status
property to be null
, why is this not the case? 我希望
Status
属性为null
,为什么不是这种情况呢?
Note: Status is not set anywhere in the constructor. 注意:状态未在构造函数中的任何位置设置。
Any integer in .net by default will hold 0 as a value. 默认情况下,.net中的任何整数都将保留0作为值。 If you need a
null
as a default value of an integer variable, you need to define it nullable. 如果需要
null
作为整数变量的默认值,则需要将其定义为可为null。 Like this- 像这样-
int? i;
More details can be found here - Using Nullable Types (C# Programming Guide) 在此处可以找到更多详细信息- 使用可空类型(C#编程指南)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.