传递到字典中的模型项的类型是' System.Data.Entity.DynamicProxies。',...需要类型为' .... IEnumerable`1]&#39的模型项;

时间:2017-12-20 11:54:35

标签: c# asp.net-mvc entity-framework asp.net-mvc-5

我写了一个MVC5项目,模型使用Code First来自数据库,这是我的问题。我使用MVC5控制器和视图,使用Entity Framework来使用Visual Studio中的默认创建,编辑,细节,删除和索引。当我运行我的项目索引和创建工作,但编辑,详细信息和删除时,它出现错误:

  

传递到字典中的模型项是类型的   ' System.Data.Entity.DynamicProxies.BaiViet_2B9E4377011DF0B03CB24AEDE01B566F2068371E02D80AC625B36ED16E6613B3&#39 ;,   但是这个字典需要一个类型的模型项   ' System.Collections.Generic.IEnumerable`1 [LoginTest.Models.BaiViet]'

MyController(QuanLyBaiVietController)

 // GET: QuanLyBaiViet/Edit/5
public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BaiViet baiViet = db.BaiViets.Find(id);
            if (baiViet == null)
            {
                return HttpNotFound();
            }

            ViewBag.C_idTheLoai = new SelectList(db.TheLoais, "C_idTheLoai", "TheLoai1", baiViet.C_idTheLoai);
            ViewBag.C_idUserDang = new SelectList(db.Users, "C_idUser", "HoTen", baiViet.C_idUserDang);
            return View(baiViet);
        }

        // POST: QuanLyBaiViet/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]

        public ActionResult Edit([Bind(Include = "C_idBaiViet,C_idTheLoai,C_idUserDang,TieuDe,TomTatNoiDung,AnhBia,NgayDang,NgayChinhSua,NoiDung,SoLanXem,TrangThaiBaiViet")]
        BaiViet baiViet, HttpPostedFileBase NoiDung, HttpPostedFileBase aAnhBia)
        {
            if (ModelState.IsValid)
            {
                db.Entry(baiViet).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.C_idTheLoai = new SelectList(db.TheLoais, "C_idTheLoai", "TheLoai1", baiViet.C_idTheLoai);
            ViewBag.C_idUserDang = new SelectList(db.Users, "C_idUser", "HoTen", baiViet.C_idUserDang);
            return View(baiViet);
        }

我的模特(BaiViet.cs)

namespace LoginTest.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("BaiViet")]
    public partial class BaiViet
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public BaiViet()
        {
            BinhLuans = new HashSet<BinhLuan>();
        }

        [Key]
        [Column("_idBaiViet")]
        public int C_idBaiViet { get; set; }

        ...        }
}

和我的观点(Edit.cshtml)

@model LoginTest.Models.BaiViet

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


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

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

            .....
    }


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

感谢您帮助我。 我不知道为什么它不起作用,因为我创建了一个只有CRUD的新项目,它正在工作,但我当前的文件发生了很大的变化

0 个答案:

没有答案