0
我是新来的asp.net MVC 5,我不知道如何使用Ajax调用具有局部视图的模态?我曾尝试过一些代码,但模态并没有显示出来。任何身体帮助? 这里是我的代码:Asp.net MVC 5 Ajax调用模态对话框
查看
<script>
@Scripts.Render("~/bundles/jqueryval")
// Create
$(".modalCreate").click(function (e) {
e.preventDefault();
$.ajax({
cache: false,
type: "GET",
url: '@Url.Action("Create","Category")',
success: function() {
$(this).attr('data-target', '#createCat');
$(this).attr('data-toggle', 'modal');
// Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
$('body').on('click', '.modal-close-btn', function() {
$('#createCat').modal('hide');
});
//clear modal cache, so that new content can be loaded
$('#createCat').on('hidden.bs.modal', function() {
$(this).removeData('bs.modal');
});
$('#CancelModal').on('click', function() {
return false;
});
// Init JQuery Validation for view
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");
},
error: function (xhr, ajaxOptions, thrownError) {
displayAjaxError(thrownError);
}
});
});
</script>
<p>
@Html.ActionLink("Create Category ", "Create", "Category",
null, new { @class = "modalCreate btn btn-success" })
</p>
<div id="createCat" class="modal fade"
tabindex="-1" role="dialog">
<div class="modal-content">
</div>
</div>
这是我的控制器:
public ActionResult Create()
{
return PartialView("_Create");
}
// POST: /Category/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CategoryCreateVM productcategory)
{
if (ModelState.IsValid)
{
ProductCategory cat = Mapper.Map<CategoryCreateVM, ProductCategory>(productcategory);
db.ProductCategories.Add(cat);
db.SaveChanges();
TempData["Message"] = "New Category added sucessfully.";
ViewBag.Message = "New Category added sucessfully.";
return RedirectToAction("Index");
}
return PartialView(productcategory);
}
和局部视图:
<div class="modal-dialog">
<div class="modal-body">
<div class="modal-content">
@using (Html.BeginForm("Create", "Category", FormMethod.Post))
{
}
</div>
</div>
</div>
我使用自举默认主题做。我尝试在浏览器检查中调试它不显示任何错误。但我可以肯定它能够找到我的部分观点,但模态从未出现。我希望任何人都可以帮我检查我的代码。