locked
Error Foreach RRS feed

  • Question

  • User-366177243 posted

    Hi, I need help for this 

    My View: 

     function CriarEventosCalendario() {
    
            var events = [];
    
            @{ 
                foreach (var item in (List<AgendaModel>)ViewBag.MostrarTodosCalendar)
                {
                    var id = @item.Id;
                    var title = @item.Title;
                    var start = @item.Start;
                    var end = @item.End;
                    var AllDay = @item.AllDay;
                    var color = @item.Color;
                    var TextColor = @item.TextColor;
                }
    
            }

    My AgendaModel: 

     public List<AgendaModel> MostrarTodosCalendar()
            {
                List<AgendaModel> lista = new List<AgendaModel>();
                AgendaModel item;
                DAL objDAL = new DAL();
                string sql = "SELECT id, title, start, end, AllDay, Color, TextColor FROM agenda where id=0";
                DataTable dt = objDAL.RetDataTable(sql);
    
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    item = new AgendaModel
                    {
                        Id = dt.Rows[i]["id"].ToString(),
                        Title = dt.Rows[i]["Title"].ToString(),
                        Start = dt.Rows[i]["Start"].ToString(),
                        End = dt.Rows[i]["End"].ToString(),
                        AllDay = dt.Rows[i]["Allday"].ToString(),
                        Color = dt.Rows[i]["Color"].ToString(),
                        TextColor = dt.Rows[i]["TextColor"].ToString()
                    };
    
                    lista.Add(item);
                }
                return lista;
            }
    

    I have this error: 

    NullReferenceException: Object reference not set to an instance of an object.

    foreach (var item in (List<AgendaModel>)ViewBag.MostrarTodosCalendar)

    Thursday, August 13, 2020 1:28 PM

Answers

  • User1686398519 posted

    Hi klasss,

    NullReferenceException

    Obviously, this is a null reference error, which means that the value of ViewBag.MostrarTodosCalendar on the view is empty. You need to check your code from the following points.

    1. Whether the returned lista in your MostrarTodosCalendar() method has a value.
    2. If the returned lista has a value, you need to pay attention to the following points:
      • The lifespan of ViewBag only starts and ends in the current request.If redirected, its value will be null.
      • In other words, does the action corresponding to your view have the ViewBag set as follows?
        • public ActionResult YourviewName()
          {
                 ViewBag.MostrarTodosCalendar = MostrarTodosCalendar();
                 return View();
          }

    Best regards,

    Yihui Sun

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 14, 2020 5:40 AM

All replies

  • User475983607 posted

    Seems the ViewBag list is null.  Double check the action and make sure it populates the ViewBag.  If the ViewBag can be null then handle this in your code.

    List<AgendaModel>)ViewBag.MostrarTodosCalendar

    Secondly, even if the ViewBag was not null the result of the foreach loop would be the last item in the list.  Seems you need to review your design.

    Thursday, August 13, 2020 3:01 PM
  • User1686398519 posted

    Hi klasss,

    NullReferenceException

    Obviously, this is a null reference error, which means that the value of ViewBag.MostrarTodosCalendar on the view is empty. You need to check your code from the following points.

    1. Whether the returned lista in your MostrarTodosCalendar() method has a value.
    2. If the returned lista has a value, you need to pay attention to the following points:
      • The lifespan of ViewBag only starts and ends in the current request.If redirected, its value will be null.
      • In other words, does the action corresponding to your view have the ViewBag set as follows?
        • public ActionResult YourviewName()
          {
                 ViewBag.MostrarTodosCalendar = MostrarTodosCalendar();
                 return View();
          }

    Best regards,

    Yihui Sun

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 14, 2020 5:40 AM
  • User1686398519 posted

    Hi klasss,

    Have your problem been solved?If you still meet problems, you can post it here and people here will be happy to solve the problem.

    Best Regards,

    YihuiSun

    Tuesday, August 18, 2020 9:58 AM