Asp.net mvc ASP.NET MVC在引导模式中的“创建”操作从单独的视图/控制器更新索引
我在这里有一个名为Reviews的视图/控制器,我想要一个引导模式弹出窗口,显示来自“ReviewChecklist”控制器的“创建”操作。这一切都显示正确,但我需要提交这篇文章以将清单数据保存到数据库中。这样做会导致它崩溃并呈现错误的页面,而不仅仅是关闭模式弹出窗口 我也有上面呈现的“ReviewChecklist”索引,因此理想情况下也应该更新 我不确定我是完全错了还是做得不对,提前谢谢 “审查”意见Asp.net mvc ASP.NET MVC在引导模式中的“创建”操作从单独的视图/控制器更新索引,asp.net-mvc,bootstrap-modal,jquery.form.js,Asp.net Mvc,Bootstrap Modal,Jquery.form.js,我在这里有一个名为Reviews的视图/控制器,我想要一个引导模式弹出窗口,显示来自“ReviewChecklist”控制器的“创建”操作。这一切都显示正确,但我需要提交这篇文章以将清单数据保存到数据库中。这样做会导致它崩溃并呈现错误的页面,而不仅仅是关闭模式弹出窗口 我也有上面呈现的“ReviewChecklist”索引,因此理想情况下也应该更新 我不确定我是完全错了还是做得不对,提前谢谢 “审查”意见 <div class="form-group"> @{
<div class="form-group">
@{
Html.RenderAction("Index", "ReviewChecklists", new { reviewId = Model.Id, viewOnly = false });
}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#checklistModal">
Launch demo modal
</button>
<div class="modal fade" id="checklistModal" tabindex="-1" role="dialog" aria-labelledby="checklistModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="checklistModalLabel">Review Checklist</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@using (Html.BeginForm("Create", "ReviewChecklistsController", FormMethod.Post, new {Id = "checklistForm" }))
{
<div class="modal-body">
@{Html.RenderAction("Create", "ReviewChecklists", new { reviewId = Model.Id });}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Save Checklist</button>
</div>
}
</div>
</div>
</div>
</div>
jQuery.Form插件已通过Nuget添加到bundle中,如下所示:
bundles.Add(new ScriptBundle("~/bundles/jqueryform").Include(
"~/Scripts/jquery.form*"));
然后在_Layout.cshtml中引用,如下所示
@Scripts.Render("~/bundles/jqueryform")
我现在面临的问题是,提交完成后,它不会关闭模式,而只是呈现页面的所有源代码?我也没有看到showRequest或showResponse的警报,但显示了表单设置的警报。1。保持HTML原样,但要从中创建的Id除外。确保它也没有嵌套在任何其他表单中
二,。将此库包含在布局中,也可以作为Nuget包提供
它是一个jQuery表单插件,允许您轻松、不唐突地升级HTML表单以使用AJAX
3.控制器
谢谢,我会在周末试一试,让你知道我的进展:是的,当然,只要你有时间:试一试,并取得了一些进展。将jQuery.Form添加为nuget包,并如上所述引用它。点击控制器并正确保存,但它不会点击showRequest或showResponse中的任何我的警报。有什么想法吗?@RyanThomas您添加了哪个库?因为我提到的图书馆不在nuget@RyanThomas那是正确的一个,你有没有给我一个身份证
bundles.Add(new ScriptBundle("~/bundles/jqueryform").Include(
"~/Scripts/jquery.form*"));
@Scripts.Render("~/bundles/jqueryform")
[HttpPost]
//[ValidateAntiForgeryToken] Causes issues with the modal dialog
public async Task<ActionResult> Create([Bind(Include =
"Id,ReviewId,ChecklistId,Status")] ReviewChecklist[] reviewChecklist)
{
foreach (ReviewChecklist item in reviewChecklist)
{
db.ReviewChecklists.Add(item);
}
await db.SaveChangesAsync();
return Json(1, JsonRequestBeahvaoir.AllowGet); /// return 1 or 0 or as per requirements or anything, the response you can handle in jQuery showResponse() method later.
}
// prepare the form when the DOM is ready
$(document).ready(function() {
var options = {
beforeSubmit: showRequest, // pre-submit callback
success: showResponse // post-submit callback
};
// bind form using 'ajaxForm'
$('#myForm1').ajaxForm(options); /// give your create form an ID
});
// pre-submit callback
function showRequest(formData, jqForm, options) {
// things you can do before form submit like loaders
return true;
}
// post-submit callback
function showResponse(responseText, statusText, xhr, $form) {
// things you can do after the response
if(responseText == 1)
$("#checklistModal").modal("hide");
// show toast or somthing for saving data successfully
}