簡體   English   中英

單擊按鈕時將部分視圖加載為模態

[英]Load partial view as modal on button click

因此,在單擊按鈕后,我嘗試加載來自控制器的部分視圖,其中包含引導模式。

我不確定問題出在哪里,但是當我第一次單擊按鈕時,添加了 DOM,但未顯示模態。 第二次單擊時,模態顯示 <1s,然后我只得到灰色模態背景。

我的按鈕

"<button class='btn btn-info edit-category' onclick='showModal()' data-category-id='" + full.id + "'>Edit</button>"

我的showModal - 功能:

function showModal(){
    $("#div1").load("/Category/EditCategory");
    $("#createCategory").modal();
}

我的局部視圖動作:

public ActionResult EditCategory()
{
    return PartialView("Admin/_CategoryAdd", new CategoryDetailModel{Title = "Test"});
}

我的 PartialView“_CategoryAdd”的一部分:

@model CategoryDetailModel

<div class="modal" id="createCategory" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <form asp-action="Create" asp-controller="Category"
                  data-ajax="true"
                  data-ajax-method="POST"
                  data-ajax-mode="replace"
                  data-ajax-failure="failed"
                  data-ajax-success="success">

有趣的是,當我不單擊按鈕並在瀏覽器控制台中輸入兩行時,它會按預期工作。 當我在控制台中執行該方法時,它與單擊按鈕相同。

我還想,也許這是一個時間問題,所以我添加了 5000 毫秒的setTimeout但沒有用。 有任何想法嗎?

在@freedomn-m 的幫助下,我發現我可以在加載完成后添加一個函數:

$("#div1").load("/Category/EditCategory", 
    function(){$("#createCategory").modal('toggle');});

現在可以了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM