我有以下代码,其中有一些我无法解决的错误。如果用户有权访问,我正在尝试向 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>
.
有两种方法可以解决此问题:
声明类以返回列表使用
public class List<NotificationOption> GetUserNotificationOptions{}
或者
ActionResult
在返回之前将您的列表转换为。

心有法竹
TA贡献1866条经验 获得超5个赞
如果您想将结果返回给客户端,您可以将JsonResult其用作一种可能的选择。
public JsonResult GetUserNotificationOptions() {
...
return Json(notificationsList);
}

BIG阳
TA贡献1859条经验 获得超6个赞
您应该将函数声明为:
public List<NotificationOption> GetUserNotificationOptions() {
否则返回变量与声明的返回类型不兼容。
- 3 回答
- 0 关注
- 149 浏览
添加回答
举报
0/150
提交
取消