搜索过滤器使用多个字段显示带有Entitiy Framework的ASP.NET MVC中的错误

时间:2019-07-03 06:44:34

标签: asp.net-mvc search linq-to-sql

我正在尝试使用多个我已使用视图模型的字段在ASP.NET MVC中应用搜索过滤器。我已经接近它了,但它显示此错误:

  

传递到字典中的模型项的类型为'System.Data.Entity.DbSet`1 [HMS.Models.tblPatient]',但是此字典需要模型项的类型为'HMS.ViewModels.SearchViewModel'

我不知道我在做什么错。

这是我的代码:

SearchController.cs

public ActionResult Index(SearchViewModel searchModel)
{
    var search = new SearchDAL();
    var model = search.GetSearchResults(searchModel);
    return View(model);
}

ViewModel.cs

public class SearchViewModel
{
    public SearchViewModel()
    {
        PatientsSearch = new List<SearchResult>();
    }

    public int? Patient_ID { set; get; }
    public string Patient_Name { set; get; }
    public string Patient_Address { set; get; }
    public string Contact_Number { set; get; }
    public int Age { set; get; }
    public string Gender { set; get; }
    public List<SearchResult> PatientsSearch { set; get; }
}

public class SearchResult
{
    public int? Patient_ID { set; get; }
    public string Patient_Name { set; get; }
    public string Patient_Address { set; get; }
    public string Contact_Number { set; get; }
    public int Age { set; get; }
    public string Gender { set; get; }
}

SearchDAL.cs

public class SearchDAL
{
    private HMS_DBEntity Context;

    public SearchDAL()
    {
        Context = new HMS_DBEntity();
    }

    public IQueryable<tblPatient> GetSearchResults(SearchViewModel searchModel)
    {
        var result = Context.tblPatients.AsQueryable();

        if (searchModel != null)
        {
            if (searchModel.Patient_ID.HasValue)
                result = result.Where(x => x.Patient_id == searchModel.Patient_ID);

            if (!string.IsNullOrEmpty(searchModel.Patient_Name))
                result = result.Where(x => x.Patient_Name.Contains(searchModel.Patient_Name));

            if (!string.IsNullOrEmpty(searchModel.Patient_Address))
                result = result.Where(x => x.Patient_address.Contains(searchModel.Patient_Address));

            if (!string.IsNullOrEmpty(searchModel.Contact_Number))
                result = result.Where(x => x.Contact_no.Contains(searchModel.Contact_Number));
        }

        return result;
    }
}

Index.cshtml

@using HMS.ViewModels
@model HMS.ViewModels.SearchViewModel
@*@model HMS.Models.tblPatient*@

@{
    ViewBag.Title = "Index";
}

<section class="content">
    @using (Html.BeginForm("Index", "Search", FormMethod.Get))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
        <div class="container-fluid">
            <div class="block-header">
                <h2>Patients Record</h2>
            </div>
            <div class="row clearfix">
                <div class="col-lg-12 col-md-12 col-sm-12">
                    <div class="card">
                        <div class="body">
                            <div class="row clearfix">
                                <div class="col-sm-6 col-md-6 col-lg-6">
                                    <div class="form-group">
                                        <div class="form-line">
                                            @Html.TextBoxFor(x => x.Patient_ID, new { @type = "Text", @class = "form-control", @id = "PatientID", @placeholder = "Patiend ID" })
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-6 col-md-6 col-lg-6">
                                    <div class="form-group">
                                        <div class="form-line">
                                            @Html.TextBoxFor(x => x.Patient_Name, new { @type = "Text", @class = "form-control", @id = "PatientName", @placeholder = "Patiend Name" })
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row clearfix">
                                <div class="col-sm-6 col-md-6 col-lg-6">
                                    <div class="form-group">
                                        <div class="form-line">
                                            @Html.TextBoxFor(x => x.Patient_Address, new { @type = "Text", @class = "form-control", @id = "PatientAddress", @placeholder = "Patient Address" })
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-6 col-md-6 col-lg-6">
                                    <div class="form-group">
                                        <div class="form-line">
                                            @Html.TextBoxFor(x => x.Contact_Number, new { @type = "Text", @class = "form-control", @id = "ContactNo", @placeholder = "Contact Number" })
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-sm-6 col-md-6 col-lg-6">
                                    <input type="submit" id="Submit" class="btn btn-raised g-bg-cyan waves-effect" value="Search" />
                                </div>

                            </div>

                        </div>



                    </div>
                </div>
            </div>
        </div>
    }

    <div class="row clearfix">
        <div class="container-fluid">
            <div class="col-lg-12 col-md-12 col-sm-12">
                <div class="card">
                    <div class="body table-responsive">
                        <table class="table table-bordered table-striped table-hover js-basic-example dataTable">
                            <tr>
                                <th>
                                    @Html.DisplayNameFor(model => model.Patient_Name)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Patient_Address)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Contact_Number)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Age)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Gender)
                                </th>
                                <th></th>
                            </tr>
                            @{
                                if (Model.PatientsSearch != null && Model.PatientsSearch.Count > 0)
                                {
                                    foreach (var item in Model.PatientsSearch)
                                    {
                            <tr>                         
                                <td>@item.Patient_Name</td>
                                <td>@item.Patient_Address</td>
                                <td>@item.Contact_Number</td>
                                <td>@item.Age</td>
                                <td>@item.Gender</td>
                            </tr>
                                    }
                                }
                            }
                        </table>
                    </div>

2 个答案:

答案 0 :(得分:0)

错误消息已清除。您在视图Index.cshtml中定义的模型是

@model HMS.ViewModels.SearchViewModel

但是您传递给视图的数据是GetSearchResults的结果,它是 System.Data.Entity.DbSet`1 [HMS.Models.tblPatient]

var model = search.GetSearchResults(searchModel);
return View(model);

我认为您现在知道如何使其工作。

答案 1 :(得分:0)

这是类型不匹配的问题:

return View(model);

因此,在GetSearchResults方法内,返回结果对象时进行以下更改:

result = new List<SearchViewModel>(result);
return result;

然后,将GetSearchResults()方法的返回类型从IQueryable更改为List

public List<SearchViewModel> GetSearchResults(SearchViewModel searchModel)