i trying make mvc 5 razor web page table within it, every row in table contains delete button post action:
<table> <thead> <tr> <th>name</th> . . . <th>actions</th> </tr> </thead> <tbody> @foreach(var item in model) { <tr> <td>@item.name</td> . . . <td> @using(html.beginform("delete", "person", formmethod.post)) { @html.antiforgetytoken() @html.hidden("personid", item.personid) <button type="submit" class="btn">delete</button> } </td> </tr> } </tbody> </table> and in controller:
public class personcontroller : basecontroller { [httppost, validateantiforgerytoken] public actionresult delete(int personid) { // something... } } now problem when pressing delete button in row of table submitting first form in page means first person in table being deleted no matter submit button press. ideas solve issue?
edit: generated page html
<div class="page-header"> <h2>person list</h2> </div> <table class="row table table-striped"> <thead> <tr class="text-primary"> <th class="text-center">#</th> <th>name</th> <th>email</th> <th>status</th> <th>action</th> </tr> </thead> <tbody> <tr> <td class="text-center">5</td> <td>ismail</td> <td>ismail@example.com</td> <td>active</td> <td> <form action="/webapp/person/delete?personid=5" method="post"> <input name="__requestverificationtoken" type="hidden" value="uxhp0bq1atawoxnodhmc74f12o2-dvfhq5kletbmdkq64cepwzlxxpkuhdoqsy4dxf6mjhyfgffc_yan1yerxp69jcut9ilgtkdfirvvvqe1" /> <div class="text-center"> <div class="btn-group btn-group-xs"> <a class="btn btn-default" href="/webapp/person/details?personid=5">view</a> <a class="btn btn-default" href="/webapp/person/edit?personid=5">edit</a> <button class="btn btn-danger>delete</button> </div> </div> </form> </td> </tr> <tr> <td class="text-center">6</td> <td>mohaka</td> <td>mohaka@example.com</td> <td>active</td> <td> <form action="/webapp/person/delete?personid=6" method="post"> <input name="__requestverificationtoken" type="hidden" value="r4cuauvpbgihzvrfxxcjcl_oj7tgks_xxh67i_xcpmxpvzkr5asuwrscvjg52yrorf-ypeau1ozwdi96cahyuj-gmbehnx7nbgfjblklpnq1" /> <div class="text-center"> <div class="btn-group btn-group-xs"> <a class="btn btn-default" href="/webapp/person/details?personid=6">view</a> <a class="btn btn-default" href="/webapp/person/edit?personid=6">edit</a> <button class="btn btn-danger>delete</button> </div> </div> </form> </td> </tr> </tbody> </table>
use instead of form code:
@using(html.beginform("delete", "person", new { item.personid })) { @html.antiforgetytoken() <button class="btn">delete</button> } asp.net mvc default action behavior: primitive types first looked in query string.
Comments
Post a Comment