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

标签 asp.net-mvc

我的 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/

相关文章:

asp.net-mvc - 用于 ASP.NET MVC 的组合框

c# - 使用 ASP.NET MVC 在 View 的 EditorFor() 方法中显示 ViewBag 的值

asp.net - SetAuthCookie 设置 Cookie,但不在后续请求中设置 IsAuthenticated

asp.net-mvc - 将所有 js 文件 bundle 在一个 bundle 中,与 css 文件相同

ajax - 参数字典在呈现局部 View 时包含参数错误的空条目

asp.net - 如何使用 AutoMapper 将多个对象映射到一个对象 - asp.net mvc 3

c# - 列表验证中的 Asp.NET 唯一项

c# - 选择语句如何从 "*"读取所有内容

c# - 查找 URL 方案

asp.net-mvc - 身份验证模式 Windows 不工作