DEV Community

mitchelln11
mitchelln11

Posted on

.Net Core Shared Link Returning Null?

I am trying to convert a .NET Framework web application to a .NET Core web application. I did start a new project, so I'm copying over info.

In the Shared Views, I am trying to link to index view inside of a Hiker folder. Inside the Hiker database, I populated 2 hikers.

I am trying to access the list with the following:

<li class="nav-item">
  <a class="nav-link text-dark" asp-area="" asp-controller="Hiker" asp-action="Index">Hikers</a>
</li>

However, I am getting the following error message:

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

Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get returned null.

It would make sense if there was no information, but that's not true. Any ideas why I'm getting an error?

Using Standard Views

Hiker Model:

public class Hiker
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Display(Name = "Street Address")]
    public string StreetAddress { get; set; }

    [Display(Name = "City")]
    public string City { get; set; }

    [Display(Name = "State")]
    public string State { get; set; }

    [Display(Name = "Latitude")]
    public string Latitude { get; set; }

    [Display(Name = "Longitude")]
    public string Longitude { get; set; }
}

The action I'm trying to get to (The out-of-the-box action):

public ActionResult Index()
{
  return View();
}

2 Hikers are populated.

Thanks for any help.

Update:
This seems to only be the case for the Index. Create, Edit, Delete, Details appear to be linking properly.

Top comments (4)

Collapse
 
mitchelln11 profile image
mitchelln11

So I did get it working for anybody else that runs across this.

Previously, the following would work in .NET Framework: (On the controller - HikerController in my case)

private ApplicationDbContext _context = new ApplicationDbContext();

Now in .NET Core, you need a constructor; starts a similar way:

private ApplicationDbContext _context;
public HikerController(ApplicationDbContext context)
{
  _context = context
}

Then you would reference database info something like this: _context.Hikers....
I have the following to display all of my hikers:

        public ActionResult Index()
        {
            return View(_context.Hikers.ToList());
        }

*If you don't use a constructor and pass in the ApplicationDbContext, you will get the following:

An unhandled exception occurred while processing the request.
InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
Collapse
 
ryanmaffey profile image
Ryan Maffey

It’s a little hard to tell but the only thing I can think of is that your view is expecting a model but you’re not providing one.

Collapse
 
mitchelln11 profile image
mitchelln11 • Edited

What I have for the Hikers view, which should display all of the hikers. Shouldn't that first line do the trick?

@model IEnumerable<walkinthepark.Models.Hiker>

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StreetAddress)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.City)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.State)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Latitude)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Longitude)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StreetAddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.State)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Latitude)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Longitude)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
}
    </tbody>
</table>

If I set up an empty view, and link to it, it gets there just fine. Once I add database info, then it fails.

UPDATE:
So I have added this since, but I am faced with another error.

private ApplicationDbContext db = new ApplicationDbContext();

        public ActionResult Index()
        {
            return View(db.Hikers);
        }

Error reads:

An unhandled exception occurred while processing the request.
InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
Collapse
 
ryanmaffey profile image
Ryan Maffey

Glad you fixed the issue with the model. Unfortunately I'm not going to be able to help with the latter issue but I'd imagine the best place to start is the Entity Framework Core docs: docs.microsoft.com/en-us/ef/core/