【发布时间】:2017-02-05 21:43:43
【问题描述】:
我有一个表格,当点击它时,应该会加载部分视图。我在 Controller 的 ActionResult RetrieveItemPrice() 中放了一个断点,它成功进入了 return 语句,但它没有在我的视图中显示 PartialView。
控制器(ItemController)
public ActionResult RetrieveItemPrice()
{
return PartialView("~/Views/Item/_ViewItemPrice");
}
查看(创建)
...
<div class="col-sm-8" style="border: 0px solid green; padding:10px;">
<div id="pvItemPrice" class="" style="border: 0px solid green; ">
</div>
</div>
...
<script type="text/javascript">
$(document).ready(function () {
$("#dbTable").click(function (e) {
debugger;
$("#pvItemPrice").load('/Item/RetrieveItemPrice');
});
});
</script>
部分视图(_ViewItemPrice)
@model CDS.Models.ItemViewModel
@using PagedList.Mvc;
<div>
<table id="dbTable2" class="table table-hover" aria-busy="false">
<thead>
<tr class="inner-table-head">
<th class="hidden">
@Html.DisplayNameFor(model => model.itemId)
</th>
<th>
@Html.ActionLink("Item", "Create", new { currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Price", "Create", new { currentFilter = ViewBag.CurrentFilter })
</th>
</tr>
</thead>
<tbody id="dbBody2">
@foreach (var item in Model.Items)
{
<tr>
<td class="hidden">
@Html.DisplayFor(modelItem => item.itemid)
</td>
<td>
@Html.DisplayFor(modelItem => item.itemdesc)
</td>
<td>
@Html.DisplayFor(modelItem => item.itemprice)
</td>
</tr>
}
</tbody>
</table>
</div>
【问题讨论】:
-
您没有将模型传递给局部视图,因此其中的代码会抛出
NullReferenceException- 您无法访问null的Items属性(我怀疑您得到了500(Internal Server Error)在浏览器中) -
哦!我现在可以理解你了!我的
JsonResult Create中有一个 LINQ,它返回 Create 中的视图。但我在ActionResult RetrieveItemPrice中没有。我对吗?顺便说一句,我没有收到500(Internal Server Error) -
但我认为至少它应该显示我的标题表。我要先尝试一下
-
现在,我尝试将 PartialView 简化为仅
<div>Hello</div>,并删除了顶部的@model代码。当我从创建视图中单击表时,它仍然没有显示将输出 Hello 的 PartialView。有什么我错过的吗? -
您会在浏览器控制台中遇到错误,因为局部视图会引发异常。如果您只用
<div>Hello</div>替换了视图中的所有代码,那么您的代码将可以正常工作(尽管我会使用return PartialView("_ViewItemPrice");)
标签: javascript asp.net-mvc view partial-views