如何在foreach循环中使用此模态?(HTML/Razor)



我有一个按钮,当点击它时,应该会打开一个模态,并确认用户是否要删除所选行,因此在模态上有一个"是"one_answers"否"选项。通过"是"按钮执行onclick功能,该功能负责删除行项目(这些操作发生在我的控制器中,不属于此问题(。为了提供一点背景,最初onclick函数是我的按钮的一部分,现在它打开了模态,这完全符合预期。然而,现在这个按钮只起到一个作用,那就是打开模态。在模态中,onclick函数现在已移动到"是"按钮。但现在,这个onclick函数不像以前那样工作了,我相信这与所有这些都在其中的foreach循环有关

以下是按钮和模式的代码:

@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++;

}
}

我遇到的问题是:onclick事件删除了文本文件中的一行,这就是为什么我有count、trigger和target来识别要删除的行。但是,在将onclick事件移动到我的模态之后,它将始终删除文本文件中的第一行。我不确定为什么会发生这种情况?我不明白语气词的含义吗?

我的控制器操作:

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);
}

根据我们的聊天,这个问题的答案是模态的ID不是唯一的,所以它创建了第一个,仅此而已。

我建议OP在模态后面加一个唯一的ID

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

这似乎奏效了。

最新更新