这是场景。我制作了一个父模型类以在一个视图中使用两个模型类。这很好,但是当我尝试从数据库中获取记录时,我收到了这个错误。传入字典的模型项的类型为“PMS_V2.Models.Pension_info”,但该字典需要类型为“PMS_V2.Models.Parentmodel”的模型项。
父模型.cs
namespace PMS_V2.Models {
// it is the class of the parent model
public class Parentmodel {
// calling of pension info
// for using foreach I have used ienumerable
public IEnumerable<Pension_info> pension_infos { get; set; }
//constructor
public Parentmodel() {
pension_infos = new List<Pension_info>();
}
// class of search
public Search id { get; set; } // method to get id
}
}
搜索.cshtml
@model PMS_V2.Models.Parentmodel
@{
ViewBag.Title = "Search";
}
<h2>Search</h2>
<p> Here Pensioner can check his records. Give your Pension_id in search query and get your details.</p>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Pension_info</legend>
<div class="editor-label">
@Html.LabelFor(model => model.id, "Pension_no")
</div>
<div class="editor-field">
@Html.TextBoxFor(model=>model.id) // getting id from user..
@Html.ValidationMessageFor(model=>model.id)
</div>
<p>
<input type="submit" value="Search" />
</p>
</fieldset>
}
<table>
@foreach (var item in Model.pension_infos) {
// model, I think the problem is here
var i = 0;
if (i == 0) {
i++;
<tr>
<th>Pension_id</th>
<th>Name</th>
<th>P_month</th>
<th>Amount</th>
</tr>
}
<tr>
<td>@Html.DisplayFor(modelItem => item.Pen_id)</td>
<td>@Html.DisplayFor(modelItem => item.Pensioner.Name)</td>
<td>@Html.DisplayFor(modelItem => item.P_month)</td>
<td>@Html.DisplayFor(modelItem => item.Amount)</td>
</tr>
}
</table>
Pension_infoController.cs
[HttpPost]
public ActionResult Search(string id) {
var abc = from m in db.Pension_info
// Linq query to get records
where m.Pen_id == id
select m;
return View(abc.FirstOrDefault());
}
请帮助我......我已经用谷歌搜索了很多......