Reputation: 656
This is my first attempt at an MVC implementation and I'm stuck trying to solve a object reference error. The data isn't getting from the Model to the ViewModel and I can't figure out why. I'm trying to pass data to a Home (Index) page but getting a "Object reference not set to an instance of an object." error because the @model isn't getting populated. I can't figure out where the "chain" is breaking. I've tried to add the relevant bits of code below.
The Model is Model/blogpost.cs, and EF created the relevant table. I added one row of data for testing.
public class BlogPosts
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string ShortPost { get; set; }
[Required]
public string Post { get; set; }
[Required]
public string Tags { get; set; }
[Required]
public DateTime Updated { get; set; }
}
the controller is Controller/HomeController.cs,
public ActionResult Index()
{
var viewModel = new IndexViewModel();
return View("Index",viewModel);
}
the view model is View/IndexViewModel.cs
public class IndexViewModel
{
public BlogPosts Blog { get; set; }
}
which is used by the View/Index.cshtml. Here is a snippet of the HTML, the fifth line from the bottom is the first use of the @model and it is NULL object :
<!DOCTYPE html>
@model congresssucks_asp.ViewModels.IndexViewModel
@{
ViewBag.Title = "BlogPosts";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="~/Content/normalize.css" />
<link rel="stylesheet" type="text/css" href="~/Content/grid.css" />
<link rel="stylesheet" type="text/css" href="~/Content/style.css" />
<link rel="stylesheet" type="text/css" href="~/Content/queries.css" />
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,300i,400" rel="stylesheet">
<meta charset="utf-8" />
<title>Congress Sucks!</title>
</head>
<body>
<section class="section-blog js-section-blog" id="latest">
<div class="row">
<h2>Most Recent Posts</h2>
</div>
<div class="row">
<div class="col span-1-of-4 box">
<div class="blog-box">
<h3>@Model.Blog.Title </h3>
<div class="blog-date">@Model.Blog.Updated</div>
<p>
@Model.Blog.ShortPost
</p>
It results in this error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 25: <div class="col span-1-of-4 box">
Line 26: <div class="blog-box">
Line 27: <h3>@Model.Blog.Title </h3>
Line 28: <div class="blog-date">@Model.Blog.Updated</div>
Line 29: <p>
I put a breakpoint on the controller, and the viewmodel object appears to be null.
Upvotes: 1
Views: 1144
Reputation: 3122
#Model class
public class BlogPosts
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string ShortPost { get; set; }
[Required]
public string Post { get; set; }
[Required]
public string Tags { get; set; }
[Required]
public DateTime Updated { get; set; }
}
#the controller is Controller/HomeController.cs
public ActionResult Index()
{
var viewModel = new IndexViewModel();
viewModel.Blog = new BlogPosts();
return View("Index",viewModel);
}
#the view model is View/IndexViewModel.cs
public class IndexViewModel
{
public BlogPosts Blog { get; set; }
}
Upvotes: 2
Reputation: 518
Your are initialize the IndexViewModel model only but BlogPosts is not initialized, See below code example and this will help you.
public ActionResult Index()
{
var viewModel = new IndexViewModel();
//viewModel.Blog <--- This object is null
viewModel.Blog = new BlogPosts(); //Get data from database or initialize an empty object(Ex: viewModel.Blog = GetPostData())
//viewModel.Blog <--- Then this object initialize as an empty object
return View("Index",viewModel);
}
Upvotes: 1