【问题标题】:mvc 3 The model item passed into the dictionary is of type A but this dictionary requires a model item of type Bmvc 3 传入字典的模型项是 A 类型,但该字典需要 B 类型的模型项
【发布时间】:2012-03-15 06:07:27
【问题描述】:

我收到以下错误:

The model item passed into the dictionary is of type '...Models.VideoPostingModel', but this dictionary requires a model item of type '...Models.RegisterModel'.

我不确定问题是什么,因为模型匹配...

控制器:

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.Mvc; 使用stayyolo.Models; 使用 System.Web.Security; 使用 System.Data;

...Controllers
{
    public class VidsPostingController : Controller
    {

        private dbEntities db = new dbEntities();
        //
        // GET: /VidsPosting/



        public ActionResult Details(Guid id)
        {
            Posting posting = db.Postings.Find(id);
            if (posting.Image == null)
            {
                posting.Image = new byte[0];
                db.Entry(posting).State = EntityState.Modified;
                db.SaveChanges();
            }
            //convert ENTITY MODEL CLASS TO Model
            VideoPostingModel toRet = new VideoPostingModel();
            toRet.linkIfVideo = posting.LinkIfVideo;
            toRet.PostDate = posting.PostDate;
            toRet.Titile = posting.Titile;
            toRet.TypeOfPosting = posting.TypeOfPosting;
            return View(toRet);
        }

}

型号:

public class VideoPostingModel
{
    ..Variables

    public VideoPostingModel()
    {
        ...

    }
}


View: 

@model ...Models.VideoPostingModel

@{
    ViewBag.Title = "Details";
}

<fieldset>
    <legend>Posting</legend>
    <div class="display-label">
        Description</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Description)
    </div>
    <div class="display-label">
        Title</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Titile)
    </div>
    <div class="display-label">
        Post Date</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.PostDate)
    </div>
    <div class="display-label">
        Type Of Posting</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.TypeOfPosting)
    </div>
</fieldset>


        <div id="youtubePlayerdiv"">
        <p style="text-align: justify">
        Video!</p>
        <iframe  id="youtubePlayer" class="youtube-player" type="text/html"
        width="640" height="385" src="http://www.youtube.com/embed/<%=Model.LinkIfVideo%>" frameborder="0">
        </iframe>
        </div>
        <!-- end youtubeplayerDiv div -->

非常感谢提前提供的帮助,当网站点击控制器的详细信息方法时,问题就出现了。

【问题讨论】:

  • 都是代码吗??你能展示一下RegisterModel吗?
  • ResgisterModel 只是您在创建项目时获得的基本 MVC 标准模型。

标签: asp.net-mvc-3 entity-framework


【解决方案1】:

您得到的错误意味着您在视图或 _Layout 的某个地方尝试渲染另一个像这样的部分:

@Html.Partial("~/Views/Account/Register.cshtml")

但是这个部分需要不同的模型 - RegisterModel。因此,一种可能性是在渲染时将此模型的新实例传递给部分:

@Html.Partial("~/Views/Account/Register.cshtml", new RegisterModel())

【讨论】:

  • 这不是问题,但我只是使用了一个 akward 解决方案并将所有数据通过视图包。谢谢你!
  • 但是如果你想出一个理由让我知道......这很奇怪——我现在不能使用模型......
猜你喜欢
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 2014-02-02
  • 2022-01-16
  • 2015-09-19
  • 1970-01-01
相关资源
最近更新 更多