locked
display data in matrix MVC ASP.net RRS feed

  • Question

  • User-1442084836 posted

    Hello i have a null reference exception when i debug, this is my code

    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>

    the null reference is at " <td>@a.FirstOrDefault(b => b.NomPn == bList.ElementAt(i)).DateDebForm</td>"

    thank's for your help

    Wednesday, November 21, 2018 1:45 PM

All replies

  • User-474980206 posted
    Because the FirstOrDefault() can return null, any property access will fail if null. Try using optional “?”


    <td>@a.FirstOrDefault(b => b.NomPn == bList.ElementAt(i))?.DateDebForm</td>
    Wednesday, November 21, 2018 3:12 PM
  • User-1442084836 posted
    Thank you for your quick reply, I already tried the expression "?" but it showed me this "?.DateDebForm" instead of the actual value of the variable.

    Ps: sorry for my bad english
    Wednesday, November 21, 2018 5:44 PM
  • User-271186128 posted

    Hi yakyine,

    I already tried the expression "?" but it showed me this "?.DateDebForm" instead of the actual value of the variable.

    Try to use the following code to check whether the query result is null:

    <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++)
            {
                var item = a.Where(b => b.NomPn == bList.ElementAt(i)).FirstOrDefault();
                if (item != null)
                {
                <td> @item.DateDebForm</td>
                }
                else
                {
                    <td>Null</td>
                }
            }
        </tr>
        }
    </table>

    Best regards,
    Dillion

    Monday, November 26, 2018 8:57 AM
  • User-1442084836 posted

    Hi Zhi Lv - MSFT,

    Thank you for you quick reply, i used you method and it works, but i have probleme here, all my tab content are Null and my data base is populated by data 

    Tuesday, November 27, 2018 9:58 AM
  • User-271186128 posted

    Hi yakyine,

    but i have probleme here, all my tab content are Null and my data base is populated by data 

    Do you mean the table content? If that is the case, you need to check the data source and the condition (especially the where condition).

    For example, I use the following data to test your code: please note the forma viewbag and NomPn value.

            public ActionResult Matrice()
            {
                List<MatriceViewModel> mylist = new List<MatriceViewModel>();
    
                ViewBag.forma = new List<string>() { "AA", "BB", "CC" };
    
                mylist = new List<MatriceViewModel>()
                {
                    new MatriceViewModel(){ DateDebForm="11/25/2018", DateFinForm="12/25/2018", ID_PNT =101001, NomPn ="AA", TypeForm ="TypeA"},
                    new MatriceViewModel(){ DateDebForm="11/26/2018", DateFinForm="12/26/2018", ID_PNT =101001, NomPn ="BB", TypeForm ="TypeB"},
                    new MatriceViewModel(){ DateDebForm="11/27/2018", DateFinForm="12/27/2018", ID_PNT =101001, NomPn ="CC", TypeForm ="TypeC"},
                };
    
                List<System.Linq.IGrouping<string, MatriceViewModel>> model = (from a in mylist group a by a.NomPn into g select g).ToList();
    
                return View(model);
            }

    The output as below:

    Best regards,
    Dillion

    Wednesday, November 28, 2018 3:20 AM