![](/img/trans.png)
[英]Partial Views don't have any style when the action has a parameter? ASP.NET MVC 5
[英]Can't create any partial views in asp.net mvc 4?
嘗試加載部分視圖時出現此錯誤,但我不知道問題是什么:
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")
}
看起來您想要執行的操作是將@Html.Partial("_rawmat")
更改為@Html.Action("Reqitem")
。 這是因為您的原始語句說直接進入視圖,並且由於您尚未傳遞模型,因此它將傳遞與包含該視圖的視圖相同的模型。
因此,如果在索引視圖上使用@Html.Partial("_rawmat")
,它將傳遞其具有OrderM
類型的OrderM
,而實際上不會調用您編寫的操作。
我不知道為什么你們的人會丟失可用的語法來調用模型的局部視圖
{@Html.RenderPartial("_PartialView",List<CDB.tblItem>) }
我假設局部視圖使用List<CDB.tblItem>
類型的Model。
如果要用任何值填充模型。 在上述語法使用之前
@{
//code to populate your model
}
如果您的局部視圖需要一個列表,則可以像這樣更改方法...
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()
添加到查詢的末尾。
我認為問題出在Index
內@Html.Partial("_rawmat")
的第二次調用中。
它沒有傳遞模型參數,因此傳遞了默認參數CDB.OrderM
。
改用它(它呈現操作而不是局部視圖):
@Html.Action("Reqitem")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.