0

I am trying to display the default index page for a model. But I get the below error.

The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[System.Boolean]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[EDIWebsite.Models.Error_Details]'.

Controller

 public ActionResult FindRelatedBols(string bolnumber)
        {
            if (bolnumber == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var error_Summaries = db.Error_Details.Select(r => r.BOL == bolnumber);
            if (error_Summaries == null)
            {
                return HttpNotFound();
            }
            return PartialView("~/Views/Error_Details/Index.cshtml",error_Summaries.ToList());
        }

View

@model IEnumerable<EDIWebsite.Models.Error_Details>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Source_System)
        </th>
.
.
.
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>
Cœur
  • 37,241
  • 25
  • 195
  • 267
null
  • 713
  • 2
  • 8
  • 30
  • You're passing it a `List`, but it's expecting an `IEnumerable`. It's not a matter of `List` to `IEnumerable`, it's what type those collections hold. – krillgar Dec 04 '17 at 17:04

1 Answers1

0

The error is self explanatory. Your view is strongly typed to a collection of Error_Details objects.Your current code is generating IQueryable<bool> as the type of error_Summaries variable and you are later calling the ToList() on that, which will generate a list of boolean values(List<bool>).

Your view is expecting something(IEnumerable<Error_Details>) and your action method is passing something else(List<bool>), hence getting that type mismatch exception!

You need to pass a collection of Error_Details objects to the view. I assume you wanted to pass a filtered list of items which is has the same BOL value as your bolnumber parameter. You can use the LINQ Where method to do the filtering.

var items =  db.Error_Details.Where(a=>a.BOL == bolnumber).ToList();
return PartialView("~/Views/Error_Details/Index.cshtml",items);

Assuming the BOL property on Error_Details class is of string type.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Hi Shyju, How do I filter the result using the bolnumber? – null Dec 04 '17 at 17:07
  • See the update i posted to the answer. – Shyju Dec 04 '17 at 17:11
  • Sorry to bug you again, could you take a peek at -https://stackoverflow.com/questions/47640100/property-on-a-model-could-not-be-set-to-a-system-decimal-value-you-must-set-t – null Dec 04 '17 at 19:11