c# - 将枚举值添加到操作内的列表中
问题描述
我有以下代码,其中有一些我无法解决的错误。如果用户有权访问,我正在尝试向 a 添加值List
,然后返回该List
.
notificationsList
当我尝试退货时,红线出现在下方。
但我不确定我是否一开始就这样做了。
public ActionResult GetUserNotificationOptions() {
List<NotificationOption> notificationsList = new List<NotificationOption>();
if(UserAccountHandler.GetIsAuthorised(_db, RestrictedArea.Audits))
{
notificationsList.Add(NotificationOption.NewAudits);
}
if (UserAccountHandler.GetIsAuthorised(_db, RestrictedArea.Overview))
{
notificationsList.Add(NotificationOption.SiginificentDeviationInScore);
}
if (UserAccountHandler.GetIsAuthorised(_db, RestrictedArea.Action))
{
notificationsList.Add(NotificationOption.OutstandingActions);
}
return notificationsList;
}
解决方案
对我来说,这看起来像是类型不匹配。我的 C# 生锈了,但看起来你的方法声明它返回一个ActionResult
,但实际上返回一个List<NotificationOption>
.
有两种方法可以解决此问题:
- 声明类以返回列表使用
public class List<NotificationOption> GetUserNotificationOptions{}
或者
ActionResult
在返回之前将您的列表转换为。
推荐阅读
- php - Woocommerce 以编程方式向产品添加属性
- python - 为什么 Python hex() 函数不适用于以 1 开头的 64 位二进制数字?
- python - Raspi I2c IOError:[Errno 121] 远程 I/O 错误
- python - Python 中使用朴素贝叶斯进行简单文本分类 - KeyError
- python - 使用谷歌 API 重复错误,但在 Pycharm 中有效
- javascript - 源图像 React Native
- c# - C# webBrowser 自动提交按钮单击不起作用
- unreal-engine4 - 在虚幻引擎 4 中,提高麦克风录音电平的最佳方法是什么?
- wpf - WPF - 数据网格 - 显示一个文本框以在选择其行时由用户设置值
- wordpress - 向 WooCommerce REST API 公开二级货币