Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC文本框不接受可为null的int为空_C#_Model View Controller_Validation - Fatal编程技术网

C# MVC文本框不接受可为null的int为空

C# MVC文本框不接受可为null的int为空,c#,model-view-controller,validation,C#,Model View Controller,Validation,模型 看法 问题: 当我将字段留空时,会得到字段大小长度必须是一个数字。。将字段留空应在返回的模型中转换为空值。为什么我的字段类型是可为空的int时会收到此消息?请尝试EditorFor,而不是TextBoxFor 不确定是否有效,因为EditorFor或TextBoxFor对象的内容是空字符串,不是空字符串,因此您必须处理转换问题,可能需要添加默认值。是的,请参见我的编辑,值不是空字符串,因此,你必须自己处理演员阵容,看看AllowEmptyStrings数据,这也没什么区别。我认为你把空字符

模型

看法

问题:

当我将字段留空时,会得到字段大小长度必须是一个数字。。将字段留空应在返回的模型中转换为空值。为什么我的字段类型是可为空的int时会收到此消息?

请尝试EditorFor,而不是TextBoxFor


不确定是否有效,因为EditorFor或TextBoxFor对象的内容是空字符串,不是空字符串,因此您必须处理转换问题,可能需要添加默认值。

是的,请参见我的编辑,值不是空字符串,因此,你必须自己处理演员阵容,看看AllowEmptyStrings数据,这也没什么区别。我认为你把空字符串转换为int?是对的?。我需要自己去做。
public int? SizeLength { get; set; }
<div class="editor-field">
    @Html.TextBoxFor(p => p.SizeLength, new { @style = "Width: 75px" })
    @Html.ValidationMessageFor(p => p.SizeLength)
</div>