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>
我使用自舉默認主題做。我嘗試在瀏覽器檢查中調試它不顯示任何錯誤。但我可以肯定它能夠找到我的部分觀點,但模態從未出現。我希望任何人都可以幫我檢查我的代碼。