简体   繁体   English

类型为List的模型的属性 <int> 尽管传递的值为null,但仍从POST实例化

[英]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.

相关问题 视图模型列表属性从发布方法变为空 - View Model List property is coming null from post method Model 列表属性为 NULL 发布在 controller - Model List property is NULL on Post in controller 从Type传递为参数的方法中实例化对象 - Instantiating Object in a method from Type passed as parameter 模型作为 null 传递给 post 方法 - model passed as null to the post method 使用 select2 在视图中传递值时列表返回 null - List returning null when value are being passed to it in view with select2 Automapper映射List中的int属性值 <type> 列表 <int> - Automapper map int property values from List<type> to List<int> 我的模型中的空对象由视图返回并传递给控制器​​,尽管它以表格形式输入 - Null object from my model returned by view and passed to controller despite entering it in the form 编辑可变长度列表。 视图模型的IEnumerable &lt;&gt;属性来自发布方法的null - Editing a variable length list. View Model IEnumerable<> property is comming null from post method "我的模型的列表属性没有从控制器传递给视图" - List properties of my model are not being passed to the View from the Controller 隐藏字段中的值未传递到EF上下文模型 - Value from hidden field is not being passed to the EF context model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM