Search code examples
c#asp.net-mvcmodel-view-controller

The model item passed into the dictionary is of type *** but this dictionary requires a model item of type ***


Hi I want to build a simple url with asp.net MVC to list and edit data from my table. I got issue at edit page. Can someone help to take a look? C# is new to me, and I created this by following a youtube tutorial

Error:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1 [MovieApp.Models.IPR_CompanyGen_200200501]', but this dictionary requires a model item of type 'MovieApp.Models.IPR_CompanyGen_200200501'.

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovieApp.Models;



 namespace MovieApp.Controllers
    {
    public class HomeController : Controller
    {
        private dtsdbEntities _db = new dtsdbEntities();
        // GET: Home
        public ActionResult Index()
        {
            return View(_db.IPR_CompanyGen_200200501.ToList());
        }
    // GET: Home/Edit/5
    public ActionResult Edit(int id)
    {
        var CompanyToEdit = (from m in _db.IPR_CompanyGen_200200501 where 
(m.CompanyID.Equals(id.ToString())) select m);
        return View(CompanyToEdit);
    }

    // GET: Home/Edit/5
    [HttpPost]
    public ActionResult Edit(IPR_CompanyGen_200200501 CompanyToEdit)
    {
        var OriginalCompany = (from m in _db.IPR_CompanyGen_200200501 where 
(m.CompanyID.Equals(CompanyToEdit.CompanyID.ToString())) select m);

        _db.Entry(OriginalCompany).CurrentValues.SetValues(CompanyToEdit);
        _db.SaveChanges();

        return RedirectToAction("Index");
    }





}

internal class MoviesDBEntities
{
}

}

Edit.cshtml

       @model MovieApp.Models.IPR_CompanyGen_200200501

    @{
        ViewBag.Title = "Edit";
    }

    <h2>Edit</h2>


    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>IPR_CompanyGen_200200501</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.CompanyID)

            <div class="form-group">
                @Html.LabelFor(model => model.CompanyName, htmlAttributes: new { @class = "control-label 
    col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CompanyName, new { htmlAttributes = new { @class = 
   "form-control" } })
                    @Html.ValidationMessageFor(model => model.CompanyName, "", new { @class = "text- 
    danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ACCOUNT_ID, htmlAttributes: new { @class = "control-label 
    col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ACCOUNT_ID, new { htmlAttributes = new { @class = 
   "form-control" } })
                    @Html.ValidationMessageFor(model => model.ACCOUNT_ID, "", new { @class = "text- 
    danger" })
                </div>
            </div>


            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

Solution

  • You forgot to use .First() or .FirstOrDefault().

    This is necessary because the LINQ query itself returns a database object System.Data.Entity.Infrastructure.DbQuery hence why we need to save to memory and store to a variable with .First(),.FirstOrDefault(),.ToList(), etc.

    public ActionResult Edit(int id)
    {
       var CompanyToEdit = (from m in _db.IPR_CompanyGen_200200501 where 
    (m.CompanyID.Equals(id.ToString())) select m);
    
       // add .FirstOrDefault()
       return View(CompanyToEdit.FirstOrDefault());
    }