为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
温温酱 2022-11-22 10:19:46
我有以下代码,其中有一些我无法解决的错误。如果用户有权访问,我正在尝试向 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;}
查看完整描述

3 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

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

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

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

或者

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


查看完整回答
反对 回复 2022-11-22
?
心有法竹

TA贡献1866条经验 获得超5个赞

如果您想将结果返回给客户端,您可以将JsonResult其用作一种可能的选择。


public JsonResult GetUserNotificationOptions() {


    ...

    return Json(notificationsList);


}


查看完整回答
反对 回复 2022-11-22
?
BIG阳

TA贡献1859条经验 获得超6个赞

您应该将函数声明为:

public List<NotificationOption> GetUserNotificationOptions() {

否则返回变量与声明的返回类型不兼容。


查看完整回答
反对 回复 2022-11-22
  • 3 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号