asp.net-mvc - View is expecting for viewmodel while List item has been passed to controller
问题描述
Error::
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ProjDAL.Models.ViewModels.EmpViewModel]', but this dictionary requires a model item of type 'ProjDAL.Models.ViewModels.EmpViewModel'.
I am working on a mvc application. For one view which basically has a functionality for multiple parameter search. I am using same view for get and post method... It means i am passing 1 or more parameters to the textboxes and fetching the result using linq and mapping to a datatable. After passing the parameters, the values are going to the controller and fetching the exact result using linq but the problem is coming when i am trying to map the linq result-set to my view.
Here is the code for the project -
Controller -
[HttpPost]
public ActionResult EmpSearch([Bind(Include = "employee,EmpID,PAN")] Get_EmpDetails_Result get_EmpDetails_Result)
{
var result = from emp in dbCollections.Employees
join nat in dbCollections.NationalID on emp.EmpID equals nat.EmpID
select new EmpViewModel
{
PAN = nat.pan,
EmpID = emp.EmpID,
employee = emp.Name
};
var searchRes = result
.Where(s => s.PAN.Contains(get_EmpDetails_Result.pan)
|| s.EmpID.Contains(get_EmpDetails_Result.empid)
|| s.employee.Contains(get_EmpDetails_Result.employee));
var modelSys = searchRes.ToList();
return View(modelSys);
}
View ::::
@model NewProjDAL.Models.ViewModels.EmpViewModel
@{
ViewBag.Title = "empDetails";
Layout = "~/Views/Shared/_Layout.cshtml";
}
//////////// this part is for the multiple criteria search
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label>By EmpID</label>
<div class="col-md-10">
@Html.EditorFor(model => model.GetEmpDetails.FirstOrDefault().empid, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
<label>By PAN</label>
<div class="col-md-10">
@Html.EditorFor(model => model.GetEmpDetails.FirstOrDefault().pan, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
<label>By Name</label>
<div class="col-md-10">
@Html.EditorFor(model => model.GetEmpDetails.FirstOrDefault().employee, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-actions center">
<button type="submit" data-animation="pulse" class="btn btn-lg font-medium-1 btn-outline-teal mb-1 block-multiple-msgs buttonAnimation">
<i class="fa fa-check-square-o"></i> Go
</button>
</div>
</div>
}
//////////////////////////////this part is to fetch the result of the previously mentioned multiple search
@if (Model.EmpDetails.Count != 0)
{
<div class="table-responsive">
<table class="table table-striped table-bordered dom-jQuery-events compact" id="DataTbl">
<thead class="navbar-dark navbar-dark bg-blue-grey white">
<tr>
<th>
employee
</th>
<th>
PAN
</th>
<th>
empid
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.GetEmpDetails)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.employee)
</td>
<td>
@Html.DisplayFor(modelItem => item.PAN)
</td>
<td>
@Html.DisplayFor(modelItem => item.empid)
</td>
</tr>
}
</tbody>
</table>
</div>
}
解决方案
它准确地告诉你问题是什么。您已将视图设置为接受单个 EmpViewModel 作为模型,但您的控制器正在传递一个列表
您需要创建一个视图模型来表示搜索条件和结果
public class EmployeeSearchViewModel
{
public string EmpId { get; set; }
public string PAN { get; set; }
public string Employee { get; set; }
public List<EmpViewModel> Employees { get; set; } = new List<EmpViewModel>();
}
然后在您的控制器方法中:
var modelSys = searchRes.ToList();
var viewModel = new EmployeeSearchViewModel
{
PAN = get_EmpDetails_Result.pan,
EmpId = get_EmpDetails_Result.empid,
Employee = get_EmpDetails_Result.employee
Employees = modelSys
};
return View(viewModel);
在视图中:
@Model EmployeeSearchViewModel
然后你的参数显示不需要从集合中拉出 EmpID、PAN 等,只需从视图模型中提取,你可以将重复的结果绑定到内部集合“Employees”。
推荐阅读
- php - 如何自动运行包的作曲家脚本
- c - 在具有 const 成员的结构上使用 memcpy 是否安全?
- jenkins - 如何通过詹金斯中的特定变更集编号下载源代码?
- php - 文档已移动 - Android Studio
- java - 在杰克逊中使用@JsonCreator 进行反序列化时,是否可以确定要构造的类型?
- javascript - 如果从某个 html 文件接近,是否有一种简单的方法可以只执行 if 语句?
- html - 在图形标签内使用标签是否正确?
- amazon-web-services - 在 S3 上创建文件文件夹
- android - 对齐卡片内的搜索图标和文本
- mysql - MySQL 服务器在没有更改后消失了