【问题标题】:The model item passed into the dictionary is of type, but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`传入字典的模型项是类型的,但此字典需要“System.Collections.Generic.IEnumerable”类型的模型项
【发布时间】:2015-09-19 21:36:35
【问题描述】:

我有两个视图,一个是会员类型列表,另一个是为会员类型创建。

两者都可以作为单独的视图正常工作,但我想将它们组合起来,以便有一个成员资格类型列表,然后是一个表单以在同一视图中创建一个。

我已将列表视图转换为部分视图以在创建视图中引用,但出现以下错误:

附加信息:传入字典的模型项是 'GRCWebApp.ViewModels.ListMembershipTypeViewModel' 类型,但是这个 字典需要类型的模型项 'System.Collections.Generic.IEnumerable`1[GRCWebApp.ViewModels.ListMembershipTypeViewModel]'。

列表控制器是:

public ActionResult ListClubMembershipType(int clubId)
{
        var types = from s in db.MembershipTypes
          where (s.ClubId == clubId)
          orderby s.Type
          select s;
    var model = types.Select(t => new ListMembershipTypeViewModel
    {
        Type = t.Type,
            ClubId = clubId
    });
        return PartialView("_ListClubMembershipType", model);    
}

创建控制器是:

public ActionResult AddClubMembershipType(NewMembershipTypeViewModel model, int clubId)
{
        model.ClubId = clubId;
        return View(model);
}

列表视图模型是:

public class ListMembershipTypeViewModel
{
    [Display(Name = "Type")]
    public String Type { get; set; }

    public int ClubId { get; set; }
}

创建的Viewmodel是:

public class NewMembershipTypeViewModel
{
    [Required]
    [Display(Name = "Type")]
    [StringLength(350, ErrorMessage = "Membership Type Name cannot be longer than 350 characters.")]
    public String Type { get; set; }

    [Display(Name = "Description")]
    [StringLength(350, ErrorMessage = "Interest Name cannot be longer than 350 characters.")]
    public String Description { get; set; }

    public int ClubId { get; set; }
}

列表视图:

@model IEnumerable<GRCWebApp.ViewModels.ListMembershipTypeViewModel>

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Type)
    </th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Type)
    </td>
</tr>
}

</table>

部分引用的创建视图是:

@model GRCWebApp.ViewModels.NewMembershipTypeViewModel

@Html.Partial("_ListClubMembershipType", new  GRCWebApp.ViewModels.ListMembershipTypeViewModel())

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

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Type, new { htmlAttributes = new {  @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Type, "", 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-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes =  new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new {    @class = "text-danger" })
        </div>
    </div>
            @Html.HiddenFor(model => model.ClubId)

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

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

【问题讨论】:

  • partialView 在ListClubMembershipType传输什么类型?
  • 创建一个包含 2 个属性的第三个视图模型 - (比如)IEnumerable MemershipList` 和 NewMembershipTypeViewModel NewMember 并将其传递给视图。或者在主视图中,使用@Html.RenderAction() 来生成partials

标签: c# asp.net-mvc


【解决方案1】:

有很多方法可以做到这一点,但为了获得最大的灵活性(允许您只渲染表单,或者只渲染集合以包含两者,那么您可以使用@Html.Action()

创建一个新的控制器方法

public ActionResult Combined(int clubId)
{
  ViewBag.ClubID = clubId;
  return View();
}

在视图中

@Html.Action("ListClubMembershipType", new { clubId = ViewBag.ClubID })
@Html.Action("AddClubMembershipType", new { clubId = ViewBag.ClubID })

并从现有的“创建”视图中删除 @Html.Partial()

注意:您的AddClubMembershipType() 不应包含NewMembershipTypeViewModel model 的参数

或者基于你现有的创建视图,将@Html.Partial()替换为

@Html.Action("ListClubMembershipType", new { clubId = Model.ClubID })

【讨论】:

  • 谢谢,我不能将你的和@Wyatt Earp 都标记为答案,因为他的引用我已经标记了。
【解决方案2】:

您的列表部分视图需要IEnumerableListMembershipTypeViewModel

@model IEnumerable<GRCWebApp.ViewModels.ListMembershipTypeViewModel>

但你只传递了一个ListMembershipTypeViewModel

@Html.Partial("_ListClubMembershipType", new  GRCWebApp.ViewModels.ListMembershipTypeViewModel())

您需要将NewMembershipTypeViewModel 更改为如下内容:

public class NewMembershipTypeViewModel
{
    [Required]
    [Display(Name = "Type")]
    [StringLength(350, ErrorMessage = "Membership Type Name cannot be longer than 350 characters.")]
    public String Type { get; set; }

    [Display(Name = "Description")]
    [StringLength(350, ErrorMessage = "Interest Name cannot be longer than 350 characters.")]
    public String Description { get; set; }

    public int ClubId { get; set; }

    public List<ListMembershipTypeViewModel> TypeList { get; set; }
}

用您要显示的项目填充控制器中的该对象,然后将视图中的行更改为:

@Html.Partial("_ListClubMembershipType", model.TypeList)

【讨论】:

  • 谢谢,请问如何在控制器中填充它?
  • @PhillSanders 无论您使用什么方法来填充其余对象。通过服务或对 DAL 的调用等...如何在 List 控制器操作中填充列表?
  • 我在列表控制器中使用它:var types = from s in db.MembershipTypes where (s.ClubId == clubId) orderby s.Type select s; var model = types.Select(t => new ListMembershipTypeViewModel { Type = t.Type, ClubId = clubId });
  • @PhillSanders 所以,在您的新控制器操作中执行此操作。 &lt;ViewModel&gt;.TypeList = from s in db.Mem... 或者看看斯蒂芬的回答,它也可以。
【解决方案3】:

这是因为您视图中的模型不是 IEnumerable。在另一种观点中,您得到了正确的结果。 从

更新部分视图中的代码
@model GRCWebApp.ViewModels.NewMembershipTypeViewModel

@model IEnumerable<GRCWebApp.ViewModels.NewMembershipTypeViewModel>

或者你必须更改 ListClubMembershipType 的控制器内容。

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2013-10-17
    • 1970-01-01
    • 2020-09-08
    • 2012-06-25
    • 2020-07-21
    • 2012-10-18
    • 2023-03-30
    • 2013-10-18
    相关资源
    最近更新 更多