【发布时间】:2015-12-30 15:50:14
【问题描述】:
我有一个使用实体框架进行数据库交互的 MVC 5 网站。
我想在控制器中使用 IEnumerable 作为私有变量,以便同一控制器中的其他自定义 ActionResult 可以使用相同的信息,而无需每次都重新查询。我不是指其他 CRUD ActionResults,而是指其他自定义方法,这些方法对索引页面上看到的数据进行处理,这通常是完整数据库表的子集。查询一次,然后重复使用相同的数据会很有帮助。
在此示例中,我将 private IEnumerable<CourseList> _data; 作为类级变量,将IEnumerable<CourseList> data 作为 Index() 级变量。我使用Debug.WriteLine 来判断每个变量是否为空。
正如我所料,在 Index() ActionResult 的范围内,两个变量 data 和 _data 都不为空。在 ClickedFromIndexPageLink() 的范围内,_data -- 类级变量为空。
我的理论是,虽然我首先按顺序加载了索引,但控制器并不知道这一点。而就控制器而言,当我在另一个ActionResult中请求_data内容时,它还没有被填充。但是,我已经实时点击了 Index,因此应该会看到 _data 被 Index 查询填充。
要查看我的解决方法,请向下滚动以查看“方法 B(确实有效,但重复)”。
有什么简单的方法可以以这种方式将 IEnumerable 用作私有类变量,还是我的解决方法是唯一可能的方法?
方法 A(不起作用):
Debug.WriteLine() 结果:
Begin Index() test *****
Is data Null? Not Null
Is _data Null? Not Null
End Index test *****
Begin ClickedFromIndexPageLink() test*****
Is _data Null? Null
End ClickedFromIndexPageLink test*****
代码:
using IenumerableAsClassVariable.Models;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Web.Mvc;
namespace IenumerableAsClassVariable.Controllers
{
// This is the helper class & function used to determine if the IEnumerable is null or empty
public static class CustomHelpers
{
public static string IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
{
if (enumerable == null)
return "Null";
else
return "Not Null";
}
}
public class CourseListsController : Controller
{
private CreditSlipLogContext db = new CreditSlipLogContext();
private IEnumerable<CourseList> _data;
// If IEnumerable is null or empty return true; else false.
// GET: CourseLists
public ActionResult Index()
{
IEnumerable<CourseList> data = db.CourseLists.AsEnumerable();
Debug.WriteLine("-----");
Debug.WriteLine("Begin Index test *****");
Debug.WriteLine("Is data Null? " + CustomHelpers.IsNullOrEmpty(data));
_data = data;
Debug.WriteLine("Is _data Null? " + CustomHelpers.IsNullOrEmpty(_data));
Debug.WriteLine("End Index test *****");
return View(db.CourseLists.ToList());
}
public ActionResult ClickedFromIndexPageLink()
{
Debug.WriteLine("Begin ClickedFromIndexPageLink test*****");
Debug.WriteLine("Is _data Null? " + CustomHelpers.IsNullOrEmpty(_data));
Debug.WriteLine("End ClickedFromIndexPageLink test*****");
ViewBag.IsDataNull = CustomHelpers.IsNullOrEmpty(_data);
return View();
}
#region OtherCrudActionResultsAreHidden
#endregion
}
}
方法 B(确实有效,但重复):
正如我所料,我的结果不是空的:
Begin ClickedFromIndexPageLink test*****
Is data Null? Not Null
Is _data Null? Not Null
End ClickedFromIndexPageLink test*****
这是因为我在 ActionResult 中重新查询,就像在 Index() ACtionResult 中一样:
public ActionResult ClickedFromIndexPageLink()
{
IEnumerable<CourseList> data = db.CourseLists.AsEnumerable();
Debug.WriteLine("Begin ClickedFromIndexPageLink test*****");
Debug.WriteLine("Is data Null? " + CustomHelpers.IsNullOrEmpty(data));
_data = data;
Debug.WriteLine("Is _data Null? " + CustomHelpers.IsNullOrEmpty(_data));
Debug.WriteLine("End ClickedFromIndexPageLink test*****");
ViewBag.IsDataNull = CustomHelpers.IsNullOrEmpty(_data);
return View();
}
【问题讨论】:
标签: c# asp.net-mvc linq