传递到字典中的模型项的类型为';System.Data.Entity.DynamicProxies.game

本文关键字:System Data game DynamicProxies Entity 类型 字典 模型 | 更新日期: 2023-09-27 18:22:33

我面临以下错误。

传递到字典中的模型项的类型为"System.Data.Entity.DynamicProxies.game_04BC2EA428E3397C72CED2755A78B93F676BBC970F6B9A8635AD53B08FEBCB",但此字典需要类型为"TeamBuildingCompetition.ViewModels.EditGameVM"的模型项

我有一个ASP.NET NVC 5 intranet应用程序。我从视图模型创建了一个编辑视图来更新数据库的内容。有问题的数据库的内容是由富文本编辑器发布的html内容。当我加载编辑视图时,它显示了上面的错误。

以下是我的编辑视图:

@model TeamBuildingCompetition.ViewModels.EditGameVM
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout_new.cshtml";
}
<script>
    tinymce.init({ selector: '#description' });
    tinymce.init({ selector: '#gameRule' });
</script>
@using (Html.BeginForm("Update", "Game", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <section id="middle">
        <div class="container">
            <div class="form-horizontal">
                <div class="center"><h1>Edit Games </h1></div>
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.gameID)
                <div class="form-group">
                    @Html.LabelFor(model => model.gameName, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.gameName, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.gameName, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.gameRule, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.gameRule, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.gameRule, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.gamePicture, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => model.gamePicture, new { @type = "file", @name = "gamePicture" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Create" class="btn btn-default" />
                    </div>
                </div>
            </div>
        </div>
    </section>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

以下是编辑视图的视图模型:

namespace TeamBuildingCompetition.ViewModels
{
    public class EditGameVM
    {
        public int gameID { get; set; }
        [Required]
        [Display(Name = "Game Name")]
        public string gameName { get; set; }
        [Required][AllowHtml]
        [Display(Name = "Description")]        
        public string description { get; set; }
        [Required]
        [AllowHtml]
        [Display(Name = "Game Rules")]
        public string gameRule { get; set; }
        [Display(Name = "Game Picture")]        
        public string gamePicture { get; set; }
    }
}

最后,这里是进行更新的控制器:

    public ActionResult Update(EditGameVM model)
    {
        try { 
        game objGame = new game
        {
            gameID = model.gameID,
            gameName = model.gameName,
            description = model.description,
            gameRule = model.gameRule,
            gamePicture = model.gamePicture.ToString()
        };            
            objBs.gameBs.Update(objGame);
            TempData["Msg"] = "Created Successfully!";
            return RedirectToAction("Edit");
        }
        catch (DbEntityValidationException dbEx)
        {
            var sb = new StringBuilder();
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    sb.AppendLine(string.Format("Entity:'{0}' Property: '{1}' Error: '{2}'",
                    validationErrors.Entry.Entity.GetType().FullName,
                    validationError.PropertyName,
                    validationError.ErrorMessage));
                }
            }
            //throw new Exception(string.Format("Failed saving data: '{0}'", sb.ToString()), dbEx);
            TempData["Msg"] = sb.ToString();
            return RedirectToAction("Edit");
        }
    }

这是我的获取方法:

    public ActionResult Edit(int id = 0) 
    {
        if (id == 0)
        {
            id = 1;
        }
        var gameList = objBs.gameBs.GetByID(id);
        return View(gameList);
    }

我将感谢为解决这一问题所做的一切努力。

传递到字典中的模型项的类型为';System.Data.Entity.DynamicProxies.game

您没有将模型发送到视图,因此发现了一个错误。可以这样做:

当您使用一些临时存储机制时,如TempData

TempData["Msg"] = objGame;
return RedirectToAction("Edit");

然后在ViewGETAction方法中再次读取它。

public ActionResult Edit()
{  
   //Here you should cast your TempData to EditGameVM:    
   EditGameVM receivedModel=TempData["Msg"] as EditGameVM;
   //To take data from TempData["Msg"], you should use receivedModel object:
   string gameID=receivedModel.gameID;
   string gameName=receivedModel.gameName;
   return View(receivedModel);
}

TempData使用场景后面的Session对象来存储数据但是一旦读取了数据,数据就会终止

我未能将模型传递给我的视图,因此出现了上述错误。在仔细检查了我的代码后,我根据https://stackoverflow.com/users/3559349/stephen-muecke劝告所有的功劳都归于这位伟人。

    public ActionResult Edit(int id = 0) 
    {
        if (id == 0)
        {
            id = 1;
        }
        var gameList = objBs.gameBs.GetByID(id);
        EditGameVM model = new EditGameVM
        {
            gameID = id,
            gameName = gameList.gameName,
            gamePicture = gameList.gamePicture,
            gameRule = gameList.gameRule,
            description = gameList.description
        };
        return View(model);
    }