提交按钮提交第一个表单页面
问题描述:
我试图让一个MVC 5剃刀网页与在它的表,在这个表中的每一行都包含一个删除按钮,这是后采取行动:提交按钮提交第一个表单页面
<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>
和Controller:
public class PersonController : BaseController
{
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Delete(int personId)
{
// Do Something...
}
}
现在我的问题是总是被删除,不管它总是提交的,这意味着在表中的第一人的页面的第一种形式的表格的任意一行按下Delete按钮时我按什么提交按钮。 任何想法来解决这个问题?
编辑: 生成的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>[email protected]</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>[email protected]</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>
答
使用此,而不是你的表单代码:
@using(Html.BeginForm("Delete", "Person", new { item.PersonId }))
{
@Html.AntiForgetyToken()
<button class="btn">Delete</button>
}
ASP.NET MVC默认操作行为:原始类型首先查询看串。
答
也许是更好的使用只是一种形式,并添加单元格这样的,而不是几种形式:
<td onclick="window.location='@Url.Action("Delete", new { PersonID = item.PersonId })'" style="cursor:pointer;">
</td>
我的眼睛可以欺骗我,但我没有看到'hidden'输入为' personId'应该是生成页面中POST负载的一部分。 – EdSF
是的,但@AlexPolyankin要求我去掉隐藏的输入并使用routeValues,它的结果是一样的 – MoHaKa
您的'controller'限制为'HttpPost'(不是'GET') - 调试您的控制器并检查什么值你得到'personId'参数)。 – EdSF