0

我的 Ilist 有问题,我无法循环遍历它,它总是抛出错误 Object reference not set to an instance of an object.. 这是我的观点.. 当我调试我的代码时,模型有一个值,但是当它转到foreach 它说 item.pillarofcultureteamlevel 没有设置..任何帮助

@model P247.Pres.MVC.ViewModel.SummaryTeamLevelViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<html>
<head>
         <title></title>
</head>
<body>
<h2>Index</h2>
@{
var item = Model;

foreach (var pillar in item.pillarofcultureteamlevel)
{
    var js = pillar.JobSecurityAndOverallCompensation;
}
}
</body>
</html>

这是我的控制器

public ActionResult Index()
    {
        _reportservice = new ReportService();
        _pillarofcultureteamlevel  = _reportservice.GetPillarsOfCultureTeamLevel(57);
        _retentiondriverteamlevel = _reportservice.GetRentionDriversTeamLevel(57);
        _employeeteamlevelsummarylist = _reportservice.GetEmployeeTeamLevelSummaryDTO(57);
        summary = new SummaryTeamLevelViewModel(_pillarofcultureteamlevel, _retentiondriverteamlevel, _employeeteamlevelsummarylist);
        return View(summary);
    }

这是我的视图模型

public class SummaryTeamLevelViewModel
{

    private IList<PillarsOfCultureTeamLevelDTO> _pillarofcultureteamlevel;
    private IList<RetentionDriverTeamLevelDTO> _retentiondriverteamlevel;
    private IList<EmployeeTeamLevelSummaryDTO> _employeeteamlevelsummarylist;

    public SummaryTeamLevelViewModel(IList<PillarsOfCultureTeamLevelDTO> pillarofcultureteamlevel,
                                    IList<RetentionDriverTeamLevelDTO> retentiondriverteamlevel,
                                    IList<EmployeeTeamLevelSummaryDTO> employeeteamlevelsummarylist)
    {
        _pillarofcultureteamlevel = pillarofcultureteamlevel;
        _retentiondriverteamlevel = retentiondriverteamlevel;
        _employeeteamlevelsummarylist = employeeteamlevelsummarylist;
    }

    public IList<PillarsOfCultureTeamLevelDTO> pillarofcultureteamlevel { get; set; }

    public IList<RetentionDriverTeamLevelDTO> retentiondriverteamlevel { get; set; }

    public IList<EmployeeTeamLevelSummaryDTO> employeeteamlevelsummarylist { get; set; }

}
4

1 回答 1

1

你的问题在你的SummaryTeamLevelViewModel课堂上。您有私有字段来存储您的数据,这就是您在控制器中调用构造函数后存储值的位置。但是您的属性不会返回或设置您的私有字段,因为您使用get; set;语法而不是显式使用您的私有字段。

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

更改您的属性,以便它们使用您的私有字段,如下所示:

public IList<PillarsOfCultureTeamLevelDTO> pillarofcultureteamlevel { get { return _pillarofcultureteamlevel; } set { _pillarofcultureteamlevel = value; } }

或更改您的构造函数并删除您的私有字段,以便您只使用您的属性,这看起来像这样:

public class SummaryTeamLevelViewModel
{
    public SummaryTeamLevelViewModel(IList<PillarsOfCultureTeamLevelDTO> pillarofcultureteamlevel,
                                    IList<RetentionDriverTeamLevelDTO> retentiondriverteamlevel,
                                    IList<EmployeeTeamLevelSummaryDTO> employeeteamlevelsummarylist)
    {
        this.pillarofcultureteamlevel = pillarofcultureteamlevel;
        this.retentiondriverteamlevel = retentiondriverteamlevel;
        this.employeeteamlevelsummarylist = employeeteamlevelsummarylist;
    }

    public IList<PillarsOfCultureTeamLevelDTO> pillarofcultureteamlevel { get; set; }

    public IList<RetentionDriverTeamLevelDTO> retentiondriverteamlevel { get; set; }

    public IList<EmployeeTeamLevelSummaryDTO> employeeteamlevelsummarylist { get; set; }

}
于 2013-01-31T06:13:10.973 回答