gpt4 book ai didi

asp.net-mvc - asp.net mvc - 当按钮名称完全相同时,如何准确找到点击了哪个按钮?

转载 作者:行者123 更新时间:2023-12-04 23:10:42 25 4
gpt4 key购买 nike

我的 aspx 文件中有以下代码:

   <% using (Html.BeginForm())
{

int i = 0;
%>

<% foreach (var item in Model.Educations)
{ %>
<fieldset>
<input type="hidden" name="educations.Index" value="" />
<p>
<label for="PID">
PID:</label>
<%= Html.TextBox("educations["+i+"].PID", item.PID)%>
<%= Html.ValidationMessage("PID", "*")%>
</p>
<p>
<label for="EducationType">
EducationType:</label>
<%= Html.TextBox("educations["+i+"].EducationType", item.EducationType)%>
<%= Html.ValidationMessage("EducationType", "*")%>
</p>
<p>
<label for="SchoolName">
SchoolName:</label>
<%= Html.TextBox("educations["+i+"].SchoolName", item.SchoolName)%>
<%= Html.ValidationMessage("SchoolName", "*")%>
</p>
<p>
<label for="UniversityId">
UniversityId:</label>
<%= Html.TextBox("educations["+i+"].UniversityId", item.UniversityId)%>
<%= Html.ValidationMessage("UniversityId", "*")%>
</p>
<p>
<label for="Department">
Department:</label>
<%= Html.TextBox("educations["+i+"].Department", item.Department)%>
<%= Html.ValidationMessage("Department", "*")%>
</p>
<p>
<label for="Degree">
Degree:</label>
<%= Html.TextBox("educations["+i+"].Degree", String.Format("{0:F}", item.Degree))%>
<%= Html.ValidationMessage("Degree", "*")%>
</p>
<p>
<label for="YearOfGraduation">
YearOfGraduation:</label>
<%= Html.TextBox("educations[" + i + "].YearOfGraduation", String.Format("{0:F}", item.YearOfGraduation))%>
<%= Html.ValidationMessage("YearOfGraduation", "*")%>
</p>
<p>
<label for="ID">
ID:</label>
<%= Html.TextBox("educations[" + i + "].ID", item.ID)%>
<%= Html.ValidationMessage("ID", "*")%>
</p>
<input type="submit" name="silButton" value="Sil"/>
</fieldset>
<%
i++;
} %>
<p>
<input type="submit" name="ekleButton" value="Ekle" />
</p>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>

如果用户想要输入其他教育信息(很可能来自另一所大学或其他学位),我可以通过这种方式动态添加(“Ekle”)更多字段。

这段代码还给出了“Sil”按钮(这意味着删除),我希望能够准确地检测到按下了哪个“Sil”按钮,并从 session 中的“对象”中删除该教育条目。

我的操作方法如下所示:
public ActionResult Step3(string ekleButton, IList<Education> educations, IList<string> silButton)
{
if (educations != null)
{
_person.Educations.Clear();
_person.Educations.AddRange(educations);
}

if (ekleButton != null)
{
_person.Educations.Add(new Education());
}

if (silButton!=null){
//find index and delete it from _person.Edications

}
return View(_person);
}

这样 silButton != null如果按下了任何“silButton”按钮,而我无法检测到按下了哪个按钮。有没有办法找出这个?

最佳答案

您可以将索引放在按钮的名称中:

<input type="submit" name="silButton<%=i %>" value="Sil" />

在您的操作方法中:
var silButton = Request.Params.AllKeys.FirstOrDefault(key => key.StartsWith("silButton"));
if (!string.IsNullOrEmpty(silButton))
{
var index = int.Parse(silButton.Replace("silButton", string.Empty));
}

关于asp.net-mvc - asp.net mvc - 当按钮名称完全相同时,如何准确找到点击了哪个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1582104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com