0

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);
        }
    }
}
2
  • I think you meant to return return View(objItemViewModel); and not the entire DbContext derived class. Commented Apr 3, 2020 at 6:26
  • That was it, thank you! Commented Apr 3, 2020 at 6:47

1 Answer 1

1

your razor page expects this model:

ItemViewModel

@model ProjectOne.ViewModel.ItemViewModel

and you're passing this to view:

return View(objECartDBEntities);

which is under ProjectOneContext

you should return the ItemViewModel

 return View(objItemViewModel);

and don't ever return this ProjectOneContext as it contains sensitive data.. you should return a custom model that only returns data that your page needs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.