【问题标题】:The model item passed into the dictionary is of type *** but this dictionary requires a model item of type ***传入字典的模型项是 *** 类型,但此字典需要 *** 类型的模型项
【发布时间】:2020-09-08 08:03:47
【问题描述】:

您好,我想用 asp.net MVC 构建一个简单的 url 来列出和编辑我的表中的数据。 我在编辑页面遇到问题。有人可以帮忙看看吗? C# 对我来说是新的,我是按照 youtube 教程创建的

错误:

传入字典的模型项是类型 'System.Data.Entity.Infrastructure.DbQuery`1 [MovieApp.Models.IPR_CompanyGen_200200501]',但此字典需要'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
{
}

}

编辑.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")
    }

【问题讨论】:

    标签: asp.net-mvc model-view-controller


    【解决方案1】:

    您忘记使用.First().FirstOrDefault()

    这是必要的,因为 LINQ 查询本身返回一个数据库对象 System.Data.Entity.Infrastructure.DbQuery,因此我们需要保存到内存并使用 .First().FirstOrDefault().ToList() 等存储到变量中。

    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());
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2015-09-19
      • 2013-10-17
      • 2012-10-18
      • 2023-03-30
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多