我有以下代碼,其中有一些我無法解決的錯誤。如果用戶有權訪問,我正在嘗試向 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 關注
- 158 瀏覽
添加回答
舉報
0/150
提交
取消
