将枚举值添加到操作内的列表

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

}


温温酱
浏览 150回答 3
3回答

三国纷争

对我来说,这看起来像是类型不匹配。我的 C# 生锈了,但看起来你的方法声明它返回一个ActionResult,但实际上返回一个List<NotificationOption>.有两种方法可以解决此问题:声明类以返回列表使用public class List<NotificationOption> GetUserNotificationOptions{}或者ActionResult在返回之前将您的列表转换为。

心有法竹

如果您想将结果返回给客户端,您可以将JsonResult其用作一种可能的选择。public JsonResult GetUserNotificationOptions() {&nbsp; &nbsp; ...&nbsp; &nbsp; return Json(notificationsList);}

BIG阳

您应该将函数声明为:public&nbsp;List<NotificationOption>&nbsp;GetUserNotificationOptions()&nbsp;{否则返回变量与声明的返回类型不兼容。
打开App,查看更多内容
随时随地看视频慕课网APP