Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net mvc - How to find exactly which button was clicked when button names are all identical?
    primarykey
    data
    text
    <p>I've got the following code in my aspx file:</p> <pre><code> &lt;% using (Html.BeginForm()) { int i = 0; %&gt; &lt;% foreach (var item in Model.Educations) { %&gt; &lt;fieldset&gt; &lt;input type="hidden" name="educations.Index" value="" /&gt; &lt;p&gt; &lt;label for="PID"&gt; PID:&lt;/label&gt; &lt;%= Html.TextBox("educations["+i+"].PID", item.PID)%&gt; &lt;%= Html.ValidationMessage("PID", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="EducationType"&gt; EducationType:&lt;/label&gt; &lt;%= Html.TextBox("educations["+i+"].EducationType", item.EducationType)%&gt; &lt;%= Html.ValidationMessage("EducationType", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="SchoolName"&gt; SchoolName:&lt;/label&gt; &lt;%= Html.TextBox("educations["+i+"].SchoolName", item.SchoolName)%&gt; &lt;%= Html.ValidationMessage("SchoolName", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="UniversityId"&gt; UniversityId:&lt;/label&gt; &lt;%= Html.TextBox("educations["+i+"].UniversityId", item.UniversityId)%&gt; &lt;%= Html.ValidationMessage("UniversityId", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Department"&gt; Department:&lt;/label&gt; &lt;%= Html.TextBox("educations["+i+"].Department", item.Department)%&gt; &lt;%= Html.ValidationMessage("Department", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Degree"&gt; Degree:&lt;/label&gt; &lt;%= Html.TextBox("educations["+i+"].Degree", String.Format("{0:F}", item.Degree))%&gt; &lt;%= Html.ValidationMessage("Degree", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="YearOfGraduation"&gt; YearOfGraduation:&lt;/label&gt; &lt;%= Html.TextBox("educations[" + i + "].YearOfGraduation", String.Format("{0:F}", item.YearOfGraduation))%&gt; &lt;%= Html.ValidationMessage("YearOfGraduation", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="ID"&gt; ID:&lt;/label&gt; &lt;%= Html.TextBox("educations[" + i + "].ID", item.ID)%&gt; &lt;%= Html.ValidationMessage("ID", "*")%&gt; &lt;/p&gt; &lt;input type="submit" name="silButton" value="Sil"/&gt; &lt;/fieldset&gt; &lt;% i++; } %&gt; &lt;p&gt; &lt;input type="submit" name="ekleButton" value="Ekle" /&gt; &lt;/p&gt; &lt;% } %&gt; &lt;div&gt; &lt;%=Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; </code></pre> <p>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). </p> <p>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.</p> <p>My action method looks like this:</p> <pre><code>public ActionResult Step3(string ekleButton, IList&lt;Education&gt; educations, IList&lt;string&gt; 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); } </code></pre> <p>In this way <code>silButton != null</code> 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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload