gpt4 book ai didi

c# - 在 View 中允许可空模型

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:23 24 4
gpt4 key购买 nike

当使用 struct/enum 或任何其他 类型时,编译器似乎显示错误我的 MVC 模型。

CS0037 Cannot convert null to 'MyEnum' because it is a non-nullable value type

我创建了一个新的 MVC 项目(在 VS 2019 中),创建了一个枚举

public enum MyEnum
{
One,
Two,
Three
}

并将其作为模型放在“关于” View 中:

enter image description here

为什么会这样?

最佳答案

The compiler appears to show a error when using a struct / value type as my MVC Model.

根据 this link ,模型绝对可以是 classstruct。有利有弊,但两者都是允许的

那么,您为什么会看到此错误?简短的回答是——这不是真正的错误。 这是一个伪造的错误

Visual Studio 有时会显示虚假错误,尤其是当 Error List 中的下拉列表设置为 Build + Intellisense(而不是 Build Only)时.

我对您的建议是将下拉列表切换为Build Only在我的项目中,Intellisense 给出了一些假错误,因此我大部分时间都将其保留在 Build Only 模式。

它出现在 Visual Studio 2017 和 2019 中。我没有在其他地方测试过它。

关于c# - 在 View 中允许可空模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56834703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com