ReturnView() method not working on C# ASP.NET MVC web application

0

I'm building a C# ASP.NET MVC WebPage to display a SQL Server data table, the method i'm getting errors in is "public ActionResult Pessoas()".

[EDIT]: So, after a debug, this is the error i got on the line 19 of my view, the foreach function:

System.NullReferenceException
  HResult=0x80004003
  Message=object reference not set to an instance of an object
  Source=App_Web_dzejebtt
  StackTrace:
   at ASP._Page_Views_Home_Index_cshtml.Execute() in C:\ConsultaPessoas\ConsultaPessoas\ConsultaPessoas\Views\Home\Index.cshtml:line 19

And as asked, this is my route file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ConsultaPessoas
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

This is my view:

@{
    ViewBag.Title = "Home Page";
}
@using ConsultaPessoas.Controllers;
@using ConsultaPessoas.Models
@model IEnumerable<Pessoa>
@using ConsultaPessoas.Controllers;


<h1>Consulta de Pessoas</h1>

<table>
    <tr>
        <th>Matricula</th>
        <th>Login</th>
        <th>Nome</th>
    </tr>
    @foreach (var pessoa in Model)
    {
        <tr>
            <td>@pessoa.Matricula</td>
            <td>@pessoa.Login</td>
            <td>@pessoa.Nome</td>
        </tr>
    }
</table>

and this is my Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Odbc;
using System.Data.Common;
using System.Data.SqlClient;
using ConsultaPessoas.Models;
using System.Web.UI.WebControls;

namespace ConsultaPessoas.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Pessoas()
        {
            String connectionString = "<Data Source = SERVER;  + Initial Catalog = DATABASE;  + User Id = User;  + Password = Password>";
            String sql = "SELECT * FROM Pessoas";
            SqlCommand cmd = new SqlCommand(sql);

            var model = new List<Pessoa>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    var pessoa = new Pessoa();
                    pessoa.Matricula = rdr["Matricula"].ToString();
                    pessoa.Login = rdr["Login"].ToString();
                    pessoa.Nome = rdr["Nome"].ToString();

                    model.Add(pessoa);
                }
                ViewBag.ListPessoa = model;
              
                return View(model);
            }
        }
    }
}

However, I wasn't able to make it work, I tried all possible changes to the application, and as soon as I run the program, I get those '/' error in application pages, with 404 code. What am I doing wrong here?

Thanks in advance!

c#
asp.net-mvc
viewbag
asked on Stack Overflow Oct 23, 2020 by Pedro Perondini • edited Oct 26, 2020 by Pedro Perondini

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0