您好,我在调试时有一个空引用异常,这是我的代码
public class MatriceViewModel
{
public string DateDebForm { get; set; }
public long ID_PNT { get; set; }
public string NomPn { get; set; }
public string TypeForm { get; set; }
public string DateFinForm { get; set; }
}
public ActionResult Matrice()
{
List<MatriceViewModel> mylist = new List<MatriceViewModel>();
ViewBag.forma = (from s in db.GetListAllStatDT().ToList() select s.TypeQuestionExam).Distinct();
mylist = db.GetListAllStatDT().Select(c => new MatriceViewModel
{
DateDebForm = c.DateDebutExam,
ID_PNT = c.ID_PNT ?? 0,
NomPn = c.Nom,
TypeForm = c.TypeQuestionExam,
DateFinForm = c.DateExpirer
}).ToList();
List<System.Linq.IGrouping<string, MatriceViewModel>> model = (from a in mylist group a by a.NomPn into g select g).ToList();
return View(model);
}
@model IEnumerable<System.Linq.IGrouping<string, BSA_TAL.Controllers.HomeController.MatriceViewModel>>
<table align="center">
@{ IEnumerable<string> bList = ViewBag.forma; }
<tr>
<td>Nom/Formation</td>
@for (int i = 0; i < bList.Count(); i++)
{
<td></td>
<td class="alert-info" style="text-align:center">@bList.ElementAt(i)</td>
}
</tr>
@foreach (var a in Model)
{
<tr>
<td class="alert-info" style="text-align:center">@a.Key</td>
@for (int i = 0; i < bList.Count(); i++)
{
<td>@a.FirstOrDefault(b => b.NomPn == bList.ElementAt(i)).DateDebForm</td>
}
}
</tr>
}
</table>
空引用位于“ @ a.FirstOrDefault(b => b.NomPn == bList.ElementAt(i))。DateDebForm”
我以相同的方法使用了另一个带有静态值和参数的矩阵,它起作用了,但是当我使用数据库时,它向我显示了空引用
对不起,我的英语不好
感谢您的帮助