Reputation: 33
I have created a custom Model in MVC where I'm passing 3 tables item as List. and in the view I'm fetching these details. But in View I'm getting Object reference not set to an instance of an object.
I'm new to MVC, can anyone help me out please. I don't know where I'm doing wrong!
My MVC Controller:
public ActionResult Index()
{
var adminModel = new AdminModel();
return View(adminModel);
}
My Custom Model Code:
public class AdminModel
{
public List<Notification> Notifications { get; set; }
public List<Places> Places { get; set; }
}
My View Code:
@model TravelFly.Models.AdminModel
@{
ViewBag.Title = "Admin Dashboard";
Layout = "~/Views/Shared/_AdminPartial.cshtml";
}
<p class="text-danger">@Model.Notifications.Count</p>
... some other contents...
Update: Controller Code:
public ActionResult Index()
{
var adminModel = new AdminModel();
return View(adminModel);
}
Class file:
public List Notifications { get; set; } = new List(); public List Places { get; set; } = new List();
Upvotes: 0
Views: 507
Reputation: 33
Actually I was not passing the data to list item I created in the custom Model. So the new controller code will be:
public ActionResult Index()
{
var adminModel = new AdminModel {
Notifications = db.Notifications.ToList(),
Places = db.Places.ToList()
};
return View(adminModel);
}
and Custom Model Class file:
public class AdminModel
{
public List<Notification> Notifications { get; set; } = new List<Notification>();
public List<Places> Places { get; set; } = new List<Places>();
}
Upvotes: 0
Reputation: 311
You should probably initialize the collections on your model or test if is null on view.
var adminModel = new AdminModel
{
Notifications = new List<Notification>(),
Places = new List<Places>()
};
OR
@if(Model.Notifications !=null)
{
<p class="text-danger">@Model.Notifications.Count</p>
}
OR
public class AdminModel
{
public List<Notification> Notifications { get; set; }
public List<Places> Places { get; set; }
public AdminModel()
{
Notifications = new List<Notification>();
Places = new List<Places>();
}
}
Upvotes: 1
Reputation: 1330
Notifications
doesn't have a default value in your AdminModel
class, so, when you're sending the new adminModel
object to your view, that property is null
and you cant call .Count
on it.
The solution depends on what you're trying to achieve, but if you want to start by avoiding the exception you could add a default value to the property by initializing it.
public class AdminModel
{
public List<Notification> Notifications { get; set; } = new List<Notification>()
public List<Places> Places { get; set; }
}
If you dont want to change that in your class you could set it's value before sending it to the view:
public ActionResult Index()
{
var adminModel = new AdminModel();
adminModel.Notifications = new List<Notification>();
return View(adminModel);
}
Upvotes: 0
Reputation: 21
Your model isn't being populated before you pass it to the view. You need to call something like this:
public ActionResult Index()
{
var adminModel = new AdminModel();
adminModel.Notifications = new List<Notifications>();
// Create a new notification [yourCreatedNotification]... then add it to the list
adminModel.Notifications.Add(yourCreatedNotification);
return View(adminModel);
}
Upvotes: 0