Reputation: 29
for (var i = 0; i < Model.PendingClasses.Count; i++)
{
<td>
@Html.DisplayFor(pc => pc.PendingClasses[i].ClassID)
</td>
<td>
@Html.DisplayFor(pc => pc.PendingClasses[i].ClassName)
</td>
<td>
@Html.ActionLink("Remove Class", "AddClass", new { RemoveClassID = Model.PendingClasses[i].ClassID }, null)
</td>
</tr>
}
I can get the correct class id above code with ActionLink. (Example - I want to remove the class id is 6, the RemoveClassID variable can pass the class id is 6.)
@using (Html.BeginForm("Remove", "RemoveClass", FormMethod.Post, htmlAttributes: new { @class = "form-horizontal" }))
{
for (var i = 0; i < Model.PendingClasses.Count; i++)
{
<td>
@Html.DisplayFor(pc => pc.PendingClasses[i].ClassID)
</td>
<td>
@Html.DisplayFor(pc => pc.PendingClasses[i].ClassName)
</td>
<td>
<input type="hidden" name="RemoveClassID" value="@Model.PendingClasses[i].ClassID" />
<button type="submit" class="btn btn-default" name="btnRemove">Remove</button>
</td>
</tr>
}
}
I can't get the correct class id above code with FormAction. (Example - I want to remove the class id is 6, the RemoveClassID variable can pass the class id is 7.)
Upvotes: 0
Views: 520
Reputation: 3002
You have a single form with multiple hidden controls and multiple submit buttons. When a submit button is clicked the entire form is submitted, so the same thing will be sent back to the server whichever button you click. To do what you want to do you will need multiple forms with one submit button per form. The following should work:
for (var i = 0; i < Model.PendingClasses.Count; i++)
{
@using (Html.BeginForm("Remove", "RemoveClass", FormMethod.Post, htmlAttributes: new { @class = "form-horizontal" }))
{
<tr>
<td>
@Html.DisplayFor(pc => pc.PendingClasses[i].ClassID)
</td>
<td>
@Html.DisplayFor(pc => pc.PendingClasses[i].ClassName)
</td>
<td>
<input type="hidden" name="RemoveClassID" value="@Model.PendingClasses[i].ClassID" />
<button type="submit" class="btn btn-default" name="btnRemove">Remove</button>
</td>
</tr>
}
}
Note I also added a <tr>
at the start of the form as you seemed to be missing one.
Upvotes: 2