加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 传入字典的ASP.NET MVC模型项是类型的

发布时间:2020-12-16 07:27:55 所属栏目:asp.Net 来源:网络整理
导读:在我看来,我遇到了这个麻烦的错误: The model item passed into the dictionary is of type ‘ContactWeb.Models.ContactListViewModel’,but this dictionary requires a model item of type ‘ContactWebLibrary.Contact’. 在这行代码中:@ {Html.Render
在我看来,我遇到了这个麻烦的错误:

The model item passed into the dictionary is of type ‘ContactWeb.Models.ContactListViewModel’,but this dictionary requires a model item of type ‘ContactWebLibrary.Contact’.

在这行代码中:@ {Html.RenderPartial(“Form”);}

我在这个文件的顶部使用@model ContactWeb.Models.ContactListViewModel.

这是我的观点:

@model ContactWeb.Models.ContactListViewModel

 <h2>Edit</h2>

 @{Html.RenderPartial("Form");}

 @using (Html.BeginForm())
 {
    <fieldset>
    <legend>Select roles for this user:</legend>
    <ul>
    @foreach(var role in Model.AllRoles)
    {
        <li><input name="Roles" type="checkbox" value="@role" />@role</li>
    }
    </ul>
    <input type="submit" />
    </fieldset>
}

解决方法

该错误告诉您必须将正确的模型传递给部分视图.
因为您没有传递任何内容,所以MVC框架使用默认模型,该模型是调用Partial模型的视图.

这将解决问题:

@{Html.RenderPartial("Form",new ContactWebLibrary.Contact());}

它与RenderPartial VS Partial无关.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读