asp.net mvc的 - 如何准确地找到哪个按钮被点击时,按钮名称都是相同的? [英] asp.net mvc - How to find exactly which button was clicked when button names are all identical?

查看:97
本文介绍了asp.net mvc的 - 如何准确地找到哪个按钮被点击时,按钮名称都是相同的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code在我的aspx文件:

I've got the following code in my aspx file:

   <% 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)更多的领域。

This way I'm able to dynamically add ("Ekle") more fields if user wants to enter additional education information (most probably from another university or another degree).

这code也给实按钮(这意味着删除),并且我希望能够准确地检测其中实按钮被pressed,并从我的对象,删除教育进入会话。

This code also give "Sil" button (which means delete), and I want to be able to detect exactly which "Sil" button was pressed and delete that Education entry from my "object" in session.

我的操作方法是这样的:

My action method looks like this:

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按钮是pressed,我无法检测哪些键是pressed。有没有办法找到这个呢?

In this way silButton != null if any of the "silButton" buttons was pressed and I can't detect which button was pressed. Is there a way to find this out?

推荐答案

您可以把指数在按钮的名称:

You could put the index in the name of the button:

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

而在你的操作方法:

And in your action method:

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的 - 如何准确地找到哪个按钮被点击时,按钮名称都是相同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆