chairmanPC
Active member
- Joined
- Apr 19, 2021
- Messages
- 38
- Programming Experience
- 10+
I have followed this site on creating a MVC ASP.NET: Create a View in ASP.NET MVC. I am new to this, so I'm familiarising myself with ASP.NET.
The first few tutorials were smooth, until I created the view page.
Last but not least, the routeConfig
URL: https://localhost:44397/staff/Index
And here, I got NullReferenceException when I test ran it. What did I miss?
The first few tutorials were smooth, until I created the view page.
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace simpleAPI.Models
{
public class Staff
{
public string staffName { get; set; }
public string staffOccupation { get; set; }
public int staffAge { get; set; }
}
}
Control:
using simpleAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace simpleAPI.Controllers
{
public class staffController : Controller
{
static IList<Staff> staffList = new List<Staff>{
new Staff() { staffName = "John", staffOccupation = "Doctor", staffAge = 18 } ,
new Staff() { staffName = "Steve", staffOccupation = "Doctor", staffAge = 21 } ,
new Staff() { staffName = "Bill", staffOccupation = "Doctor",staffAge = 25 } ,
new Staff() { staffName = "Ram" , staffOccupation = "Doctor", staffAge = 20 } ,
new Staff() { staffName = "Ron" , staffOccupation = "Doctor", staffAge = 31 } ,
new Staff() { staffName = "Chris" , staffOccupation = "Doctor", staffAge = 17 } ,
new Staff() { staffName = "Rob" , staffOccupation = "Doctor", staffAge = 19 }
};
// GET: busModel
public ActionResult Index()
{
return View();
}
}
}
View:
@model IEnumerable<simpleAPI.Models.Staff>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.staffName)
</th>
<th>
@Html.DisplayNameFor(model => model.staffOccupation)
</th>
<th>
@Html.DisplayNameFor(model => model.staffAge)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.staffName)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffOccupation)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffAge)
</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>
Last but not least, the routeConfig
RouteConfig:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace simpleAPI
{
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 = "staff", action = "Index", id = UrlParameter.Optional }
);
}
}
}
URL: https://localhost:44397/staff/Index
And here, I got NullReferenceException when I test ran it. What did I miss?