Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc - How to find exactly which button was clicked when button names are all identical?

Tags:

asp.net-mvc

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>

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).

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

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?

like image 411
Azho KG Avatar asked Jan 24 '23 02:01

Azho KG


1 Answers

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));
}
like image 82
Darin Dimitrov Avatar answered Feb 03 '23 17:02

Darin Dimitrov