This is the error that I get:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'ProjectOne.Data.ProjectOneContext', but this ViewDataDictionary instance requires a model item of type 'ProjectOne.ViewModel.ItemViewModel'.
It may or may not be related but I cannot even connect to the database without directly typing in my OnConfiguring line into my code. I can usually put it in User Secrets, appsettings.json, or a seperate class file. But it's not working in appsettings.json at least.
I'm not sure what exactly is going on here. I'll post my code down below.
Can someone please help me out with this one? Thank you!
DBContext:
namespace ProjectOne.Data
{
public class ProjectOneContext : DbContext
{
public ProjectOneContext(){}
public ProjectOneContext(DbContextOptions<ProjectOneContext> options) : base(options){}
public DbSet<ProjectOne.Models.Categories> Categories { get; set; }
}
}
ItemViewModel:
@model ProjectOne.ViewModel.ItemViewModel
@{
ViewBag["Title"] = "Index";
}
<h1>Index</h1>
<div class="container">
<div class="col-md-4">
<div class="form-group">
Category:
@Html.DropDownListFor(expression: model => model.CategoryId, new SelectList(Model.CategorySelectListItem, dataValueField: "Value", dataTextField: "Text"), htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
Item Code:
@Html.TextBoxFor(expression: model => model.ItemCode, htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
Item Name:
@Html.TextBoxFor(expression: model => model.ItemName, htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
Description:
@Html.TextBoxFor(expression: model => model.Description, htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
Item Price:
@Html.TextBoxFor(expression: model => model.ItemPrice, htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
Image Path:
@Html.TextBoxFor(expression: model => model.ImagePath, htmlAttributes: new { type = "file", @class = "form-control" })
</div>
</div>
</div>
Item Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using ProjectOne.Data;
using ProjectOne.ViewModel;
namespace ProjectOne.Controllers
{
public class ItemController : Controller
{
private readonly ProjectOneContext objECartDBEntities;
public ItemController()
{
objECartDBEntities = new ProjectOneContext();
}
public IActionResult Index()
{
ItemViewModel objItemViewModel = new ItemViewModel();
objItemViewModel.CategorySelectListItem = (from objCat in objECartDBEntities.Categories
select new SelectListItem()
{
Text = objCat.CategoryName,
Value = objCat.CategoryId.ToString(),
Selected = true
});
return View(objECartDBEntities);
}
}
}
View(objItemViewModel);and not the entire DbContext derived class.