MVC AJAX搜索结果在部分视图中

时间:2016-11-30 11:12:33

标签: asp.net-mvc

我试图做一些我之前没有做过的事情,我尝试了几种不同的方法来实现它,但仍然遇到问题让它发挥作用而不确定我是否正确方式。

我有许多不同的错误消息,但这是当前的消息,当我单击搜索按钮并且它尝试返回结果时显示(我只能看到这是开发人员工具):

  

传递到字典中的模型项的类型为' System.Collections.Generic.List' 1 [PGS.Areas.Admin.Models.ViewModels.SuppliersViewModel]',但此字典需要类型' PGS.Areas.Case.Models.VisitReportModel'的模型项。

我有很长的表格(所以我不会发布所有代码);在同一页面上,我需要一个搜索表单,它将使用AJAX从部分视图返回结果(最终用户将选择一个搜索结果,并将其ID插入主表单的隐藏字段中,但我“此刻并不担心。”

主视图使用:VisitReportModel

我已经为结果中显示的每一位数据添加了属性并保存结果(我不确定这是否正确):

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

单击搜索按钮时,它会运行操作:

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

}

在部分视图中返回结果:

@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>

0 个答案:

没有答案
相关问题