传递到字典中的模型项的类型为'System.Data.Entity.DynamicProxies.game [英] The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.game

查看:73
本文介绍了传递到字典中的模型项的类型为'System.Data.Entity.DynamicProxies.game的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脸部出现以下错误.

传递到字典中的模型项的类型为'System.Data.Entity.DynamicProxies.game_04BC2EA428E3397C72CED2755A5A78B93F676BBC970F6B9A8635AD53B08FEBCB',但是此字典需要的模型项类型为'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");
        }
    }

这是我的Get方法:

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

我将竭尽全力解决这个问题.

解决方案

我无法将模型传递给我的视图,因此出现上述错误.在仔细检查我的代码后,我根据> 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);
    }

I have the following error staring me in the face.

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.game_04BC2EA428E3397C72CED2755A5A78B93F676BBC970F6B9A8635AD53B08FEBCB', but this dictionary requires a model item of type 'TeamBuildingCompetition.ViewModels.EditGameVM'

I have an ASP.NET NVC 5 intranet application. I created an edit view from a view model to update contents of my database. Content of the database in question is an html content which was posted in by a rich text editor. When I load the edit view, it shows the above error.

Below is my edit view:

@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")
}

Below is the View Model for the Edit View:

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

And finally, here's the controller to do the update:

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

Here's my Get Method:

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

I will appreciate all effort to resolving this.

解决方案

I failed to pass the model to my view hence, the above error. After careful examination of my code, I did the following according to https://stackoverflow.com/users/3559349/stephen-muecke advice. All credit goes to this great guy.

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

这篇关于传递到字典中的模型项的类型为'System.Data.Entity.DynamicProxies.game的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆