.net - 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 的新手。
解决方案
<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
操作方法有关,请尝试在此方法中设置断点,并逐步调试您的代码。
推荐阅读
- go - dep init 上的“不在已知的 GOPATH/src 中”错误
- javascript - 如何使用材料下拉菜单获取所选值?
- actions-on-google - TRANSACTION_REQUIREMENTS_CHECK 不起作用
- node.js - 如何让机器人在首次加入服务器时说出特定消息?
- java - 测试计划开始/结束时的 JMeter 方法调用
- c++ - 在 C++ 中内联外部汇编函数
- ms-access - 访问表达式:计算多列中的唯一值
- python - 来自 tkintet 文本的变量值的总和
- apache-spark - 必须使用 writeStream.start() 执行带有流源的查询
- java - 怎么修?变量 with 从内部类中访问,需要是最终的或有效的最终的