在我的MVC Core应用程序中,我想要在提交表单后加载部分视图。asp.net MVC Core - 使用Ajax加载部分视图
提交_TransferForm
中的表单后,将加载另一个局部视图,而不是刷新整个页面。以下是我的代码。
为什么_ApplySuccess
占据整个页面,而不是作为页面的一部分显示在Apply
?
我在共享_Layout
中添加了<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js">
。
父视图Apply.cshtml
:
@Html.Partial("_ApplyForm")
@section Scripts {
<script>
// delegate submit event to document, since form might be replaced
$(document).on('submit', '#applyForm', function() {
var $theForm = $(this);
// manually trigger validation
if ($theForm.valid()) {
$('#progress').show();
$.post('', $theForm.serialize())
.done(function (response) {
$theForm.replaceWith(response);
})
}
return false;
});
</script>
}
局部视图_ApplyForm.cshtml
:
<form id="applyForm" asp-action="Apply" asp-controller="Jobs">
// content
</form>
局部视图_ApplySuccess.cshtml
:
<div>Successfully applied.</div>
控制器的动作Apply
:
public async Task<IActionResult> Apply(Applicant applicant)
{
if (ModelState.IsValid)
{
// code
return PartialView("_ApplySuccess");
}
return PartialView("_ApplyForm");
}
可能的重复[如何在脚本部分中包含脚本包在视图中](http://stackoverflow.com/questions/13660868/how-to-include-script-bundle-in-scripts-section-in-view ) –
@ebramkhalil这没有帮助。顺便提一下,我编辑了我的问题。 – Matt