【问题标题】:Partial View not being loaded by Javascript when table is clicked单击表格时,Javascript未加载部分视图
【发布时间】: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 - 您无法访问 nullItems 属性(我怀疑您得到了 500(Internal Server Error)在浏览器中)
  • 哦!我现在可以理解你了!我的JsonResult Create 中有一个 LINQ,它返回 Create 中的视图。但我在ActionResult RetrieveItemPrice 中没有。我对吗?顺便说一句,我没有收到500(Internal Server Error)
  • 但我认为至少它应该显示我的标题表。我要先尝试一下
  • 现在,我尝试将 PartialView 简化为仅&lt;div&gt;Hello&lt;/div&gt;,并删除了顶部的@model 代码。当我从创建视图中单击表时,它仍然没有显示将输出 Hello 的 PartialView。有什么我错过的吗?
  • 您会在浏览器控制台中遇到错误,因为局部视图会引发异常。如果您只用&lt;div&gt;Hello&lt;/div&gt; 替换了视图中的所有代码,那么您的代码将可以正常工作(尽管我会使用return PartialView("_ViewItemPrice");

标签: javascript asp.net-mvc view partial-views


【解决方案1】:

模型在哪里被传递到视图(CDS.Models.ItemViewModel)?

return PartialView("~/Views/Item/_ViewItemPrice");

另外,网络工具会显示什么作为响应? 200 还是 500 响应代码?

【讨论】:

  • 您在回答问题吗?我认为你应该向 OP 展示 如何通过局部视图传递模型
  • 我现在可以看到我的ActionResult RetrieveItemPrice() 中没有 LINQ。谢谢! @Div 是对的。而且,我认为它至少应该显示表格标题。我没有为此使用任何模型。我会回复你们的
  • 试图简化我的 PartialView 以查明问题,现在它只有
    Hello
    代码。但是当我点击Create中的表格时它仍然没有显示@
  • 如果视图创建的任何部分失败,则它不会返回视图中编译的内容。您在视图中还有对模型等的引用吗?
猜你喜欢
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多