简体   繁体   English

我如何在我的 foreach 循环中使用这个模式? (HTML / 剃刀)

[英]how can i use this modal within my foreach loop? (HTML / Razor)

I have a button that when clicked should open a modal with a confirmation stating whether the user would like to delete the selected line or not, hence on the modal, there is a 'Yes' and 'No' option.我有一个按钮,单击该按钮应打开一个模式,并确认用户是否要删除选定的行,因此在模式上,有一个“是”和“否”选项。 With the 'Yes' button carrying out the onclick function which is responsible for deleting the line item (These operations take place within my controller and are not part of this question).使用“是”按钮执行 onclick function 负责删除行项目(这些操作发生在我的 controller 中,不属于此问题)。 To provide a bit of background, originally the onclick function had been part of my button, which now opens the modal, and this worked perfectly as intended.为了提供一些背景知识,最初 onclick function 是我的按钮的一部分,现在它打开了模式,这完全符合预期。 However, now that button serves only one purpose, and that is to open the modal.但是,现在该按钮仅用于一个目的,那就是打开模式。 Within the modal, the onclick function has now been moved to the 'Yes' button.在模态中,onclick function 现在已移至“是”按钮。 But now, this onclick function does not work as it use to, and i believe this is something to do with the foreach loop that all of this is in.但是现在,这个 onclick function 不能像以前那样工作,我相信这与所有这些都在其中的 foreach 循环有关。

Below is the code for the button and modal:下面是按钮和模式的代码:

@Model.CategoryList.result
        @if (Model.CategoryList.result == "")
        {
            int count = 0;

            foreach (String dataLine in Model.CategoryList.userData)
            {

                string countString = count.ToString();
                string target = "dataLine" + countString;
                string trigger = "#" + target;
                string deleteHolder = dataLine.Split(Model.CategoryList.delimiterChar)[0];

                <p>
                    <a data-toggle="collapse" href="@trigger" role="button" aria-expanded="false" aria-controls="collapseExample">
                        @dataLine.Split(Model.CategoryList.delimiterChar)[0]
                    </a>

                    <button class="btn" data-toggle="modal" data-target="#deleteHolder" id="@dataLine.Split(Model.CategoryList.delimiterChar)[1]"></button>

                </p>

               <div class="modal" id="deleteHolder" 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>
                            <div class="modal-body">
                                <p>Modal body text goes here.</p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary" onclick="location.href = '@Url.Action("DeleteCategoryLine", "Index", new { id = dataLine.Split(Model.CategoryList.delimiterChar)[1] })'">Yes</button>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                            </div>
                        </div>
                    </div>
                </div>

                count++;


            }
        }

The problem i have is: The onclick event deletes a line in the text file, that is why i have count, trigger and target to identify WHICH line to delete.我遇到的问题是:onclick 事件删除了文本文件中的一行,这就是为什么我有计数、触发器和目标来识别要删除的行。 However, after moving the onclick event to my modal, it will always delete the first line in the text file.但是,将 onclick 事件移动到我的模态后,它总是会删除文本文件中的第一行。 I am unsure as to why this happens?我不确定为什么会这样? Do i not understand the implications a modal presents?我不理解模态呈现的含义吗?

My controller Action:我的 controller 操作:

public ActionResult DeleteCategoryLine(int id, string changedItem)
        {
            string strFilePath = "~/App_Data/Category.txt";
            string strSearchText = id.ToString();
            string strOldText;

            string n = changedItem;
            StreamReader sr = System.IO.File.OpenText(Server.MapPath(strFilePath));
            while ((strOldText = sr.ReadLine()) != null)
            {
                string[] x = strOldText.Split(',');

                if (!x[1].Contains(strSearchText))
                {
                    n += strOldText + Environment.NewLine;
                }
            }
            sr.Close();
            System.IO.File.WriteAllText(Server.MapPath(strFilePath), n);
}

As per our chat, the answer to this issue was that the ID of the modal was not unique and so it was creating the first and that's that.根据我们的聊天,这个问题的答案是模态的 ID 不是唯一的,所以它创建了第一个,就是这样。

I advised OP to suffix the modal with a unique ID我建议 OP 使用唯一 ID 为模式添加后缀

deleteHolder_@dataLine.Split(Model.CategoryList.delimiterChar)[1]

and it seems to have worked.它似乎奏效了。

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

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