小编Shi*_*ror的帖子

删除[必需]属性MVC 5后,"必需"验证消息仍然存在

[Required]在我的一个视图模型的属性中有一个属性:

[DefaultValue(1)]
[Required(ErrorMessage = "* Required")] // this has now been removed
public int QuoteQuantity { get; set; }
Run Code Online (Sandbox Code Playgroud)

我删除了[Required],但我仍然收到此验证消息,阻止我提交.

在视图中我有这些线:

@Html.EditorFor(model => model.QuoteQuantity, new { htmlAttributes = new { @class = "form-control", value = "1" } })
@Html.ValidationMessageFor(model => model.QuoteQuantity, "", new { @class = "text-danger" })
Run Code Online (Sandbox Code Playgroud)

当我将其留空并提交时,我得到此验证错误:

QuoteQuantity字段是必需的.

我应该提一下,我已经重复构建了解决方案,关闭并重新打开了VS,即使当前代码是这样,我仍然不断收到此验证错误:

[DefaultValue(1)]
public int QuoteQuantity { get; set; }
Run Code Online (Sandbox Code Playgroud)

知道为什么会这样吗?

c# validation asp.net-mvc asp.net-mvc-5

5
推荐指数
1
解决办法
1925
查看次数

LINQ查询返回错误“预期的类型为'System.Int32',但实际值为null。”

所以这是查询:

from c in Contents
join cs in DepartmentSharings 
on c.ContentId equals cs.ContentId
select c.PrivateCategoryId.Value
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到:

处理请求时,数据库操作失败。InvalidOperationException:读取属性'DepartmentSharing.ContentId'的数据库值时发生异常。预期的类型为“ System.Int32”,但实际值为空。

现在,我检查了DepartmentSharings.ContentId数据库中的字段fk, int, not null。在课堂上是public int ContentId { get; set; }DepartmentSharing.ContentId不能是null。另外,在LinqPad中,我可以看到它在失败之前返回了58行。为什么会出现此错误?

c# linq asp.net-core-2.0

4
推荐指数
1
解决办法
5133
查看次数