在我的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