question

Mel-8940 avatar image
0 Votes"
Mel-8940 asked Mel-8940 commented

System.NullReferenceException: 'Object reference not set to an instance of an object.' when trying to run new View.

Can someone please have a look at my code and help me understand why I'm getting this error? I have set up a controller and model, but when trying to run this view I get this error.244080-2022-09-23.png


dotnet-aspnet-mvc
2022-09-23.png (183.1 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

View:

 @model IEnumerable<BasicMVCProject.Models.Client>
    
 @{
     ViewBag.Title = "Index";
 }
    
 <h2>Index</h2>
    
 <p>
     @Html.ActionLink("Create New", "Create")
 </p>
 <table class="table">
     <tr>
         <th>
             @Html.DisplayNameFor(model => model.FirstName)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.LastName)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.Phone)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.Email)
         </th>
         <th></th>
     </tr>
    
 @foreach (var item in Model) {
     <tr>
         <td>
             @Html.DisplayFor(modelItem => item.FirstName)
         </td>
         <td>
             @Html.DisplayFor(modelItem => item.LastName)
         </td>
         <td>
             @Html.DisplayFor(modelItem => item.Phone)
         </td>
         <td>
             @Html.DisplayFor(modelItem => item.Email)
         </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>
 }
    
 </table>

The view is actually called Index.cshtml and is within the Client folder under view folders.

0 Votes 0 ·

And model:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
    
 namespace BasicMVCProject.Models
 {
     public class Client
     {
         public string FirstName { get; set; }
         public string LastName { get; set; }
         public int Phone { get; set; }
         public string Email { get; set; }
    
     }
 }
0 Votes 0 ·

1 Answer

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered Mel-8940 commented

Hi @Mel-8940,

System.Web.Mvc.WebViewPage<TModel>.Model.get returned null

The error is because you haven't defined Model in your View.
You need to fill your model and send it to the View.
Try the following:
.cshtml

 @model MVCname.Models.Your Model 

Controller

 public ActionResult Index()
 {
     using (MvcDB dataBase = new MvcDB())
     {
    
     }
     return View(< YourModel > );
 }

If possible, you can provide code that reproduces the problem so we can better help you troubleshoot.

Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your prompt answer Lan! I did try inputting Client in the return View(); but I got this message: Client is a type which is not valid given the context.

I have set up ia Client model, ClientController and Client.cshtml view. I followed a YouTube tutorial so I'm just so stuck!

Here is the controller code:
using BasicMVCProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

 namespace BasicMVCProject.Controllers
 {
     public class ClientController : Controller
     { 
         // GET: Client
         public ActionResult Index()
         {
            
             List<Client> clientList = new List<Client>();
    
             clientList.Add(new Client { FirstName = "John", LastName = "Williams", Phone = 0421762441, Email = "john@yahoo.com" });
             clientList.Add(new Client { FirstName = "John", LastName = "Williams", Phone = 0421762441, Email = "john@yahoo.com" });
             clientList.Add(new Client { FirstName = "John", LastName = "Williams", Phone = 0421762441, Email = "john@yahoo.com" });
             clientList.Add(new Client { FirstName = "John", LastName = "Williams", Phone = 0421762441, Email = "john@yahoo.com" });
    
         return View();
         }
     }
 }

Here is the view:

...continued


0 Votes 0 ·

Hi @Mel-8940,
According to your code, what you need to return is clientList.

  return View(clientList);

Best regards,
Lan Huang

0 Votes 0 ·
Mel-8940 avatar image Mel-8940 LanHuang-MSFT ·

That worked! Thank you so much for your help :)

0 Votes 0 ·