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>