[英]Url.Action not hitting controller method
我有一个表,其中包含指向控制器中方法的链接。 该表可在带有提交按钮的表单中找到。
以下是我的看法:
@using (Html.BeginForm("SaveClient", "Client", FormMethod.Post))
{
@Html.AntiForgeryToken()
string language = ViewBag.Language;
<div class="table-wrapper">
<table width="100%">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Client.ClientCode)
</th>
<th>
@Html.DisplayNameFor(model => model.Client.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Client.Surname)
</th>
<th>
@Html.DisplayNameFor(model => model.Client.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Client.ContactNumber)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ClientMatches)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ClientCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.ContactNumber)
</td>
<td>
<button onclick="location.href='@Url.Action("ShowClient", "Client", new { id=item.ClientCode})'">
<i class="icon-arrow-right"></i>
</button>
</td>
</tr>
}
</tbody>
</table>
</div>
@Html.HiddenFor(m => m.Client.Surname, Model.Client.Surname)
@Html.HiddenFor(m => m.Client.Name, Model.Client.Name)
@Html.HiddenFor(m => m.Client.ContactNumber, Model.Client.ContactNumber)
@Html.HiddenFor(m => m.Client.Email, Model.Client.Email)
@Html.HiddenFor(m => m.Client.Comments, Model.Client.Comments)
<div class="mdl-card__actions centered">
<button>
Create New
</button>
</div>
}
我在ClientController中的ShowClient方法:
public ActionResult ShowClient(string clientCode)
{
if (clientCode == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
*some other actions*
}
当我单击“新建”按钮时,此按钮正常工作。 但是,当我单击表中的Url.Action时,我的ShowClient方法没有被点击。 知道我可能做错了什么吗? 谢谢
只需使用@Html.ActionLink()
:
@Html.ActionLink("ShowClient", "ShowClient", "Client", new { id=item.ClientCode }, new { @class = "btn btn-primary btn-sm" })
对于更复杂的类型,例如添加自定义图像或图标,您可以使用<a>
标记,或者对于您的示例 ,您可以使用Font Awesome的方向性图标 。
您需要使用定位标记而不是按钮,
实际上,您在表单中有按钮,因此当您单击按钮时,它将提交表单,并且onclick操作将不会执行,
<a href="@Url.Action("ShowClient", "Client", new { clientCode=item.ClientCode})">
<i class="icon-arrow-right"></i>
</a>
并且查询字符串参数应该是clientCode
而不是id
因为您在操作方法中具有带有clientCode
参数名称
另一建议:您需要删除表单,因为在这里您不提交任何东西,因此视图的目的只是显示记录并浏览特定的ClientCode
,
并添加一个使用锚创建的链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.