if (errors.Count > 0) {
foreach(var error in errors) {
List < JsonReportErrors > Results = new List < JsonReportErrors > ();
new JsonReportErrors {
ErrorKind = error.kind, ErrorPath = error.path
};
}
List < ReturnJsonErrors > ReturnJson = new List < ReturnJsonErrors > ();
new ReturnJsonErrors {
Success = false, Errors = Results
};
return Json(ReturnJson, JsonRequestBehavior.AllowGet);
}
我试图访问new ReturnJsonErrors { Success = false, Errors = Results };
声明中foreach之外的结果列表
但继续
Results does not exist in this context.
答案 0 :(得分:0)
您的变量仅存在于声明的上下文中。在您的情况下,它变为&#34;生活&#34;仅在foreach
循环内。为了使它在它之外可见,你必须在它之外声明它:
List<JsonReportErrors> Results = new List<JsonReportErrors>();
foreach (var error in errors)
{
new JsonReportErrors { ErrorKind = error.kind, ErrorPath = error.path };
}
另外,您可能希望将创建的JsonReportErrors
对象添加到此列表中,对吧?但那是一个不同的问题......