Я пытаюсь применить фильтр поиска в 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>