尝试加载部分视图时出现此错误,我不知道问题所在:
System.InvalidOperationException:传递给的模型项 字典的类型为'CDB.OrderM',但这个字典需要一个 型号项目 'System.Collections.Generic.IEnumerable`1 [CDB.tblItem]'。
public ActionResult Index()
{
OrderM om = new OrderM();
List<tblItem> tList = db.Query<tblItem>("Select * from tblItem").ToList<tblItem>();
ViewBag.tList = tList;
return View(om);
}
public ActionResult Reqitem()
{
//tblItem ti= db.Query<tblItem>("select * from tblItem");
var ti = db.Query<tblItem>("select * from tblItem");
return PartialView("_rawmat",ti);
}
My Partial View codes are-
@model IEnumerable<CDB.tblItem>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemId)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemName)
</th>
<th>
@Html.DisplayNameFor(model => model.MeasuringUnit)
</th>
<th>
@Html.DisplayNameFor(model => model.Rate)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.BagSz)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ItemName)
</td>
<td>
@Html.DisplayFor(modelItem => item.MeasuringUnit)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.BagSz)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
和索引视图代码是......
@model CDB.OrderM
@{
ViewBag.Title = "Index";
var tList = ViewBag.tList;
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>OrderM</legend>
<div class="editor-label">
@Html.LabelFor(model => model.OdrId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OdrId)
@Html.ValidationMessageFor(model => model.OdrId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OrderNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OrderNo)
@Html.ValidationMessageFor(model => model.OrderNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OdrDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OdrDate)
@Html.ValidationMessageFor(model => model.OdrDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OdrQty)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OdrQty)
@Html.ValidationMessageFor(model => model.OdrQty)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OdrAmount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OdrAmount)
@Html.ValidationMessageFor(model => model.OdrAmount)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DDate)
@Html.ValidationMessageFor(model => model.DDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CId)
@Html.ValidationMessageFor(model => model.CId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Pod)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Pod)
@Html.ValidationMessageFor(model => model.Pod)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
@Html.Partial("_rawmat")
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:2)
您想要做的就是将@Html.Partial("_rawmat")
更改为@Html.Action("Reqitem")
。这是因为你的原始陈述说直接进入视图,因为你没有传递模型,它将传递与它所包含的视图相同的模型。
因此,如果您在索引视图中使用@Html.Partial("_rawmat")
,它将传递它具有OrderM
类型的模型,而不会实际调用您已写入的操作。
答案 1 :(得分:1)
我不知道为什么人们错过了使用模型调用部分视图的可用语法
{@Html.RenderPartial("_PartialView",List<CDB.tblItem>) }
我假设局部视图使用类型List<CDB.tblItem>
的模型。
如果要使用任何值填充模型。就在上面的语法之前使用
@{
//code to populate your model
}
答案 2 :(得分:0)
如果你的部分视图需要一个List,那么改变这样的方法......
public ActionResult Reqitem()
{
//tblItem ti= db.Query<tblItem>("select * from tblItem");
var ti = db.Query<tblItem>("select * from tblItem").ToList();//notice to List
return PartialView("_rawmat",ti);
}
我已将.ToList()
添加到查询的末尾。
答案 3 :(得分:-2)
我认为问题出在@Html.Partial("_rawmat")
内Index
的第二次调用中。
它不传递模型参数,因此它传递了默认值CDB.OrderM
。
改为使用它(它渲染动作而不是局部视图):
@Html.Action("Reqitem")