Show data from table in view asp.net mvc 5 (NullReferenceException)

0

I want to show some data from my table into a view. I followed steps from other stackoverflow posts but it didn't work. I get a null NullReferenceException: System.Web.Mvc.WebViewPage.Model.get returned null.

I tried to change my sql select string. Different models and more but i didn't found the solution

Model

public class IndexViewModel
    {
        public bool HasPassword { get; set; }
        public IList<UserLoginInfo> Logins { get; set; }
        public string PhoneNumber { get; set; }
        public bool TwoFactor { get; set; }
        public bool BrowserRemembered { get; set; }
        public string Id { get; set; }
        public string Points { get; set; }
        public string ToetsenGemaakt { get; set; }
        public string VragenGoed { get; set; }
        public string VragenFout { get; set; }
        public float VerhoudingGoedFout { get; set; }
        public string Day { get; set; }
        public bool PointsDay { get; set; }
        public bool PointsNewUser { get; set; }
    }

Controller:

public ActionResult IndexViewModel()
        {
            string connectionString = "DefaultConnection";
            string sql = "SELECT * FROM AspNetUserStatics WHERE Id=" + User.Identity.GetUserId();
            SqlCommand cmd = new SqlCommand(sql, con);

            var model = new List<IndexViewModel>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    var statics = new IndexViewModel();
                    statics.Id = rdr["Id"].ToString();
                    statics.Points = rdr["Points"].ToString();
                    statics.ToetsenGemaakt = rdr["ToetsenGemaakt"].ToString();
                    statics.VragenGoed = rdr["VragenGoed"].ToString();
                    statics.VragenFout = rdr["VragenFout"].ToString();

                    model.Add(statics);
                }

            }

            return View(model);
        }

View:

@{
    ViewBag.Title = "Home Page";
}
@model List<Leermoment.Models.IndexViewModel>

<!-- Statistics -->
<div class="row">
    <!-- Level -->
    <div class="col-lg-6 invisible" data-toggle="appear">
        <div class="block block-bordered">
            <div class="block-content">
                <div class="px-sm-3 pt-sm-3 clearfix" style="min-height: 260px;">
                    <i class="fa fa-chart-line fa-2x text-gray-light float-right"></i>

                    @foreach (var indexViewModel in Model)
                    {
                <p class="display-4 text-black font-w300 mb-2">
                    @indexViewModel.Points <span class="font-size-h5 font-w600 text-muted">POINTS</span>
                </p>
                    }

                    <p class="text-muted w-75">
                        Je heb <strong>15</strong> points verdiend vandaag. Je doet het goed, blijf het goed doen en hou dit vast!
                    </p>
                    <a class="btn btn-hero-sm btn-outline-primary btn-square mr-1 mb-1" href="javascript:void(0)">
                        <i class="fa fa-play mr-1"></i> Toetsen
                    </a>
                    <a class="btn btn-hero-sm btn-outline-primary btn-square mr-1 mb-1" href="@Url.Action("Index", "Manage")">
                        <i class="far fa-user fa-fw mr-1"></i> Account
                    </a>
                </div>
            </div>


//and more code below that doesn't matter anymore like </divs>

I got no errors but when i run the website i get a null exception.

It looks like this:

System.NullReferenceException
  HResult=0x80004003
  Message=De objectverwijzing is niet op een exemplaar van een object ingesteld.
  Source=App_Web_2b4putnl
  StackTrace:
   at ASP._Page_Views_Home_Index_cshtml.Execute() in C:\Users\games\source\repos\Leermoment\Leermoment\Views\Home\Index.cshtml:line 15
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

I can't find the issue and i am very new in asp.net mvc. Hopefully someone know the issue. There is a other thread of a null exception and i think i know wat a null exception is. But i can't figure out how to fix the exception. The Model from @foreach (var indexViewModel in Model) gives the null exception but how can i fix it?

Kind Regards, Bart

c#
sql
asp.net-mvc
listview
asked on Stack Overflow Jun 2, 2019 by Bart Brouwers • edited Jun 3, 2019 by Bart Brouwers

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0