首页 > 解决方案 > Partialview 未渲染并在 .Net Core 中返回错误 500

问题描述

我正在尝试通过 index.cshtml 中的表单标记 ajax 调用来呈现局部视图。不知道为什么使用IE11调试工具调试partialview总是显示500错误。ajax 调用确实有效,因为 javascript 函数“onFailed”显示消息“失败:对象对象”。

下面是 index.cshtml 代码:

> <script>
    var onFailed = function (context) {
        alert("Failed: " + context);
    };
</script>

               <form asp-controller="ProcTracking" asp-action="EpSearchResults" id="epsForm"
                  data-ajax-update="#divSearch" data-ajax-mode="replace"
                  data-ajax-begin="onBegin" data-ajax-complete="onComplete"
                  data-ajax-failure="onFailed" data-ajax-success="onSuccess"
                  data-ajax="true" data-ajax-method="GET">

...... (for brevity)

                            <td>
                                <button type="submit" onclick="return checkEntries()" class="btn btn-primary rounded btn-sm text-center"><span aria-hidden="true" class="fa fa-search"></span> Search</button>
                            </td>
                            <td>
                                <button type="button" onclick="resetForm()" class="btn btn-warning rounded btn-sm text-center"><span aria-hidden="true" class="fa fa-refresh"></span> Reset</button>
                            </td>


</form>

        <div id="divSearch" class="mx-auto">

        </div>

下面是控制器代码:(调试时在 VS 调试器中调用此操作)

>      [HttpGet]
        public IActionResult EpSearchResults(IFormCollection form, List<string> pi_status, List<string> bud_qtr, int Search_cat = 0, int Search_type = 0, int Search_mfg = 0)
        {
            TempData["xsearch"] = form["R1"];

            TempData["xpcat"] = Search_cat;

            TempData["xptype"] = Search_type;

            TempData["xpmfg"] = Search_mfg;

...... (for brevity)

            return PartialView("_EpSearchResults");
        }

下面是_EpSearchResults.cshtml:(只有一行文本用于调试目的,仍未显示)

>     <h4 id="Nodata" class="text-center">You have made it!</h4>

注意:所有这些东西都适用于 MVC 5。我正在将其转换为 .Net 5 (Core)。我已经花了 2 天的时间,但仍然没有到达任何地方。我是 .Net Core 的新手。

标签: .netasp.net-core

解决方案


  <form asp-controller="ProcTracking" asp-action="EpSearchResults" id="epsForm"
                  data-ajax-update="#divSearch" data-ajax-mode="replace"
                  data-ajax-begin="onBegin" data-ajax-complete="onComplete"
                  data-ajax-failure="onFailed" data-ajax-success="onSuccess"
                  data-ajax="true" data-ajax-method="GET">

在 asp.net core 应用中使用data-ajax-*加载局部视图,在 View 页面中,我们仍然需要添加jquery.unobtrusive-ajax引用,代码如下:

<form asp-controller="Home" asp-action="EpSearchResults" id="epsForm"
      data-ajax-update="#divSearch" data-ajax-mode="replace"
      data-ajax-begin="onBegin" data-ajax-complete="onComplete"
      data-ajax-failure="onFailed" data-ajax-success="onSuccess"
      data-ajax="true" data-ajax-method="GET" >
     
    <button type="submit" onclick="return checkEntries()" class="btn btn-primary rounded btn-sm text-center">
        <span aria-hidden="true" class="fa fa-search"></span> Search
    </button>

    <button type="button" onclick="resetForm()"
            class="btn btn-warning rounded btn-sm text-center">
        <span aria-hidden="true" class="fa fa-refresh"></span> Reset
    </button> 
</form>

<div id="divSearch" class="mx-auto">

</div>
@section Scripts{
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ajax-unobtrusive/3.2.6/jquery.unobtrusive-ajax.min.js"></script>
    <script> 
        var onFailed = function (context) {
            alert("Failed: " + context);
        };
        function checkEntries() {
            console.log("Check entities")
        }
        function onBegin() {
            console.log("Begin");
        }
        function onComplete() {
            console.log("complete");

        }
        function onSuccess() {
            console.log("success");
        }
    </script>
}

结果如下:

在此处输入图像描述

对于 500 错误,可能与EpSearchResults操作方法有关,请尝试在此方法中设置断点,并逐步调试您的代码。


推荐阅读