简体   繁体   中英

The model item passed into the dictionary is of type 'requires a model item of type 'System.Collections.Generic.IEnumerable`

I have created a web application in mvc3 and created two partial views one having controls like dropdownlist. second having webgrid which shows data from database.

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

        }

How can i show webgrid and populate dropdown with same Index()??

getting error :( what should be 2nd parameter inside @Html.Partial() so that both grid and control works fine on same page.?

You are passing a SecurityIdentifierMapping model to the Index view. Inside this Index view you are calling 2 partials:

@Html.Partial("_ControlsPartial")

and:

@Html.Partial("_WebGridPartial")

The first one works fine because it is strongly typed to SecurityIdentifierMapping but the second one (the one with the grid) doesn't work because it is strongly typed to IEnumerable<SecurityIdentifierMapping> . Thus the exception you are getting.

I would recommend you using a view model which will contain 2 properties: one simple SecurityIdentifierMapping that you could pass to the first partial and an IEnumerable<SecurityIdentifierMapping> property that you will pass to the second partial. It is the controller action that will fill this view model and pass it to the Index view:

Index.cshtml :

@model MyViewModel

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question The model item passed into the dictionary is of type '` but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable' The model item passed into the dictionary is of type .. but this dictionary requires a model item of type System.Collections.Generic.IEnumerable' The model item passed into the dictionary is of type, but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable The model item passed into the dictionary is of type , but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable The model item passed into the dictionary is of type, but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable` The model item passed into the dictionary is of type , but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable The model item passed is of type 'System.Collections.Generic.List but this dictionary requires 'System.Collections.Generic.IEnumerable The model item passed into the dictionary is of type 'System.Collections.Generic.List', but requires 'System.Collections.Generic.IEnumerable Requires a model item of type 'System.Collections.Generic.IEnumerable`1 How can I fix item passed into dictionary is type 'Models', but dictionary requires model of type 'System.Collections.Generic.IEnumerable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM