【问题标题】:The model item passed into the dictionary is of type 'requires a model item of type 'System.Collections.Generic.IEnumerable`传递到字典中的模型项的类型为“需要一个类型为“System.Collections.Generic.IEnumerable”的模型项
【发布时间】:2012-04-24 00:47:02
【问题描述】:

我在 mvc3 中创建了一个 Web 应用程序并创建了两个局部视图 一个具有下拉列表等控件。 第二个有 webgrid,它显示来自数据库的数据。

partialview1.cshtml
@model Mapping.Models.SecurityIdentifierMapping

@using (Html.BeginForm("Mapping", "Home"))
{
    @Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
    <br />    
    @Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
    <br />
    <button type="submit">Map</button>
}

partialview2.cshtml
@model IEnumerable<Mapping.Models.SecurityIdentifierMapping>

@{
    ViewBag.Title = "Mapping";
    WebGrid grid = null;
    if (Model.Count() > 0 ){
    grid = new WebGrid(source: Model,
                            defaultSort: "Id",
                            canPage: true,
                            canSort: true,
                            rowsPerPage:20);
    }
}


<h2>Mapping</h2>


@if (grid != null)
{
@grid.GetHtml(
                tableStyle: "grid",
                headerStyle: "head",
                alternatingRowStyle: "alt",
                columns: grid.Columns(
                                            grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Edit", new { id = (int)item.id })  @Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
                                            grid.Column("PricingSecurityID"),
                                            grid.Column("CUSIP")
                                          )

                )
}
<br />
<p>
    @Html.ActionLink("Back", "Index")
</p>

in index.cshtml


<div>
@Html.Partial("_ControlsPartial",)
</div>

<div>
@Html.Partial("_WebGridPartial")
</div>

HomeController.cs
  public ActionResult Index()
        {
            SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
            objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
            objModel.CUSIP = objRepository.GetCUSIP();
            return View(objModel);

        }

如何显示 webgrid 并使用相同的 Index() 填充下拉列表?

得到错误:( @Html.Partial() 中的第二个参数应该是什么,以便网格和控件在同一页面上正常工作。?

【问题讨论】:

  • 任何内部的 MVC 专家请给我任何提示?
  • 当前页面的型号是什么类型?
  • IEnumerable
  • 和其他问题是我如何使用相同的索引方法来绑定下拉菜单并显示 webgrid ???

标签: asp.net-mvc asp.net-mvc-3 model-view-controller


【解决方案1】:

您正在将 SecurityIdentifierMapping 模型传递给 Index 视图。在此索引视图中,您调用了 2 个部分:

@Html.Partial("_ControlsPartial")

和:

@Html.Partial("_WebGridPartial")

第一个工作正常,因为它被强类型化为SecurityIdentifierMapping,但第二个(带有网格的)不起作用,因为它被强类型化为IEnumerable&lt;SecurityIdentifierMapping&gt;。因此你得到了例外。

我建议您使用包含 2 个属性的视图模型:一个简单的 SecurityIdentifierMapping 可以传递给第一个部分,一个 IEnumerable&lt;SecurityIdentifierMapping&gt; 属性可以传递给第二个部分。控制器操作将填充此视图模型并将其传递给索引视图:

Index.cshtml:

@model MyViewModel

<div>
    @Html.Partial("_ControlsPartial", Model.SecurityIdentifier)
</div>

<div>
    @Html.Partial("_WebGridPartial", Model.Identifiers)
</div>

【讨论】:

  • 1.什么是'Model.Identifiers'?和 2.我应该在两个局部视图和 index.cshtml 中使用哪个 @model 语句? 3. Index() 方法我应该保持原样吗?我如何选择然后显示 webgrid 数据?
  • IdentifiersIEnumerable&lt;SecurityIdentifierMapping&gt; 类型的视图模型上的属性。 2. 在局部视图中,您可以保持与当前相同的@model。在Index.cshtml 内,您可以使用@model MyViewModel,如我的回答所示。 3. 由于包含 webgrid 的部分被强类型化为IEnumerable&lt;SecurityIdentifierMapping&gt;,您可以将模型直接用作网格的数据源。剩下的就是让您的控制器操作实例化视图模型并填充 2 个属性,即准备视图所需的数据。
  • inside index.cshtml in Index() 方法我正在做 View(dbContext.SecurityIdentifierMappings.ToList()) 这给了我错误:传递到字典中的模型项的类型是'System. Collections.Generic.List1[Mapping.Models.SecurityIdentifierMapping]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Mapping.Models.SecurityIdentifierMappingViewModel]'。
  • 是的,我已经在回答中涵盖了这部分内容。你需要一个视图模型。
猜你喜欢
  • 2013-10-17
  • 1970-01-01
  • 2012-06-25
  • 2022-01-16
  • 2015-09-19
  • 2015-12-19
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多