简体   繁体   English

由于 javascript 对话框,asp-route 始终对每个循环使用第一个 Id html

[英]asp-route always uses the first Id of html for each loop because of javascript dialog

I am displaying data from my c# backend with a for each loop in the html page.我正在显示来自我的 c# 后端的数据,并在 html 页面中使用 for each 循环。 When deleting an element, I am opening a modal dialog to confirm.删除元素时,我打开一个模式对话框进行确认。 But because this involves javascript, the delete function after the confirmation always is the one for the first item of my data.但是因为这里涉及到javascript,确认后删除的function始终是我数据的第一项。 So it seems that the calling of javascript restarts the for each loop.I always get the first id in the backend.所以似乎 javascript 的调用重新启动了每个循环。我总是在后端获得第一个 id。

The relevant html code:相关html代码:

<form method="post">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        </thead>
        <tbody>
        @foreach (var experiment in Model.MainExperimentsTests)
        {
            <tr>
                <td>@experiment.Id</td>
                <td>@experiment.ExperimentTestName</td>
                <td>
                    <a class="btn permissionRead permissionWrite"
                       onclick="showDeleteDialog()">
                        <i class="bi-bucket"></i>
                    </a>
                    <dialog id="deleteDialog">
                        Do you really want to delete?
                        <button asp-page-handler="Delete" asp-route-experimentId="@experiment.Id"
                        value="confirm">Ok</button>
                        <button onclick="closeDeleteDialog()" type="reset">Cancel</button>
                    </dialog> </td>

            </tr>
        }

        </tbody>
    </table>
</form>

Javascript code: Javascript 代码:

<script>
    function showDeleteDialog() {
        document.getElementById("deleteDialog").showModal();
    }
    function closeDeleteDialog() {
        document.getElementById("deleteDialog").close();
    }
</script>

I tried saving the id in javascript and doing an ajax post call from there instead of using the "asp-page-handler" and "asp-route-experimentId", but I couldn't get it to work properly.我尝试将 id 保存在 javascript 中,并从那里进行 ajax 后调用,而不是使用“asp-page-handler”和“asp-route-experimentId”,但我无法使其正常工作。 I tried different ways to open a dialog, but I don't want the default pop up from the browser, I want my own modal dialog.我尝试了不同的方式来打开对话框,但我不想从浏览器中弹出默认对话框,我想要我自己的模式对话框。

Does anyone know how I can get the right id to be passed to asp-route?有谁知道我如何才能获得传递给 asp-route 的正确 ID? Is my structure completely wrong?我的结构完全错误吗? I appreciate your help.我感谢您的帮助。

You need unique dialog ids.您需要唯一的对话 ID。 For example you can concat experiment id to the dialog id.例如,您可以将实验 ID 连接到对话 ID。 Try something like the following:尝试以下操作:

<a class="btn permissionRead permissionWrite" onclick="showDeleteDialog(@experiment.Id)">
    <i class="bi-bucket"></i>
</a>
<dialog id="deleteDialog_@experiment.Id">
   ...
    <button onclick="closeDeleteDialog(@experiment.Id)" type="reset">Cancel</button>
</dialog> 

<script>
    function showDeleteDialog(experimentId) {
        document.getElementById("deleteDialog_" + experimentId).showModal();
    }
    function closeDeleteDialog(experimentId) {
        document.getElementById("deleteDialog_" + experimentId).close();
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM