0

I'm trying to do something that I’ve not done before and I’ve tried a few different ways of achieving it but still having issues getting it to work and not sure I’m going about it the right way.

I’ve had a number of different error messages but this is the current one, which displays when I click the search button and it attempts to return the results (I can only see this is Developer Tools):

The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[PGS.Areas.Admin.Models.ViewModels.SuppliersViewModel]', but this dictionary requires a model item of type 'PGS.Areas.Case.Models.VisitReportModel'.

I have quite a long form (so I won’t post all the code just yet); on the same page I need a search form that will use AJAX to return results from a partial view (Eventually the user will select one of the search results and the ID of that will be inserted into a hidden fields of the main form, but I’m not worried about that at the moment) .

The main View uses: VisitReportModel

To which I’ve added properties for each bit of data to display in the results and to hold the results (I’m not sure this is correct):

public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string PostCode { get; set; }
public string Country { get; set; }
public string Tel { get; set; }
public string Mobile { get; set; }
public string Fax { get; set; }
public string Email { get; set; }
public IEnumerable<SuppliersViewModel> SearchResultsList { get; set; }

When the search button is clicked it Runs Action:

public ActionResult SupplierSearch(int? CatId, string searchName, string searchTown, string searchCounty, string searchPostcode)
{

    var Results = (from sup in efContext.Suppliers
                    join type in efContext.SupplierCategories
                    on sup.CategoryId equals type.Id
                    where (sup.Name.Contains(searchName) || searchName == null)
                    && (sup.Town.Contains(searchTown) || searchTown == null)
                    && (sup.County.Contains(searchCounty) || searchCounty == null)
                    && (sup.PostCode.Contains(searchPostcode) || searchPostcode == null)
                    && (sup.CategoryId == CatId || CatId == null)
                    orderby sup.Name
                    select new VisitReportModel
                    {
                        Id = sup.Id,
                        CategoryId = sup.CategoryId,
                        CategoryName = type.Category,
                        Name = sup.Name,
                        Address1 = sup.Address1,
                        Address2 = sup.Address2,
                        Address3 = sup.Address3,
                        Town = sup.Town,
                        County = sup.County,
                        PostCode = sup.PostCode,
                        Country = sup.Country,
                        Tel = sup.Tel,
                        Mobile = sup.Mobile,
                        Fax = sup.Fax,
                        Email = sup.Email,
                        Enabled = sup.Enabled
                    }).ToList();

    return PartialView("~/Areas/Case/Views/Partials/SupplierSearch.cshtml", Results);

}

Which returns results in Partial View:

@model PGS.Areas.Case.Models.VisitReportModel
@using PGS.Utilities


<script src="~/Scripts/App/scripts.js"></script>

<p>@ViewBag.FormResults</p>

<table class="table">
    <thead>
        <tr>
            <th colspan="10">
                <span class="record-count">@ViewBag.RecordCount</span>
            </th>
        </tr>
        <tr>
            <th>Company Name</th>
            <th>Category</th>
            <th>Address</th>
            <th>Town</th>
            <th>County</th>
            <th>Postcode</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.SearchResultsList)
        {
            <tr>
                <td>
                    @Html.DisplayFor(model => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(model => item.CategoryName)
                </td>
                <td>
                    @Extensions.SupplierShortAddressFormat(item.Address1, item.Town, item.County, item.PostCode)
                </td>
                <td>
                    @Html.DisplayFor(model => item.Town)
                </td>
                <td>
                    @Html.DisplayFor(model => item.County)
                </td>
                <td>
                    @Html.DisplayFor(model => item.PostCode)
                </td>
            </tr>
        }
    </tbody>

</table>
Jammer
  • 2,330
  • 11
  • 48
  • 77
  • Your view is expecting a `VisitReportModel` yet you're passing it a `List`? – Ryan Searle Nov 30 '16 at 11:15
  • The View or PartialView? I've tried both: model PGS.Areas.Case.Models.VisitReportModel and model IEnumerable in the partial view and both error. – Jammer Nov 30 '16 at 11:19
  • and when you did you won't get the same error message. – Ryan Searle Nov 30 '16 at 11:21
  • You have not shown the relevant code. Your `SupplierSearch()` method returns a `List` which has nothing to do with the error. You have shown a partial view which accepts a single `VisitReportModel` so that view is not the `SupplierSearch.cshtml` view you refer to in the method (otherwise it would be a different error message - _The model item passed into the dictionary is of type List[VisitReportModel] ..._ . You need to debug your code and find which line of code is throwing that error –  Nov 30 '16 at 11:22
  • Hi Stephen, thanks, i've just looked at the code and I had made a stupid mistake and hadn't changed the Partial name from some older code so was calling the wrong partial, so am not getting a different error, i'll see how I get on now. – Jammer Nov 30 '16 at 11:25
  • @Stephen I've got it working now, must have had it correct hours ago but at some point accidently reverted my code back to get the old PartialView which was causing the error but I didn't spot it! – Jammer Nov 30 '16 at 11:51

0 Answers0