使用具有有价值框架的ASP.NET MVC中显示错误的多个字段搜索过滤器



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

传递到字典中的模型项是'system.data.entity.dbset`1 [hms.models.tblpatient]'的类型,但是该词典需要类型'hms.viewmodels.searchviewmodels.searchviewmodel'/blockquote>

我不知道我在做什么错。

这是我的代码:

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>

错误消息清晰。您在视图index.cshtml中定义的模型是

@model HMS.ViewModels.SearchViewModel

但是,您传递给视图的数据是GetSearchResults的结果,它是 System.data.entity.dbset`1 [hms.models.tblpatient]

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

我想你知道如何使它现在起作用。

这是一个类型的不匹配问题:

return View(model);

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

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

,然后将您的返回类型从iQueryable更改为getsearchResults((方法

public List<SearchViewModel> GetSearchResults(SearchViewModel searchModel)

相关内容

最新更新