首页 > 解决方案 > 将枚举值添加到操作内的列表中

问题描述

我有以下代码,其中有一些我无法解决的错误。如果用户有权访问,我正在尝试向 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#listmodel-view-controlleraction

解决方案


对我来说,这看起来像是类型不匹配。我的 C# 生锈了,但看起来你的方法声明它返回一个ActionResult,但实际上返回一个List<NotificationOption>.

有两种方法可以解决此问题:

  1. 声明类以返回列表使用public class List<NotificationOption> GetUserNotificationOptions{}

或者

  1. ActionResult在返回之前将您的列表转换为。

推荐阅读