C# 操作未命中控制器方法
我有一个表,其中包含指向控制器中某个方法的链接。 该表位于带有“提交”按钮的表单中 以下是我的看法:C# 操作未命中控制器方法,c#,get,url.action,C#,Get,Url.action,我有一个表,其中包含指向控制器中某个方法的链接。 该表位于带有“提交”按钮的表单中 以下是我的看法: @using (Html.BeginForm("SaveClient", "Client", FormMethod.Post)) { @Html.AntiForgeryToken() string language = ViewBag.Language; <div class="table-wrapper">
@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>
}
当我点击“创建新”按钮时,它工作正常。但是当我点击表中的Url.Action时,我的ShowClient方法没有被点击。
你知道我做错了什么吗?
谢谢您需要使用锚定标签而不是按钮 实际上,表单中有一个按钮,因此当您单击按钮时,它将提交表单,而您的onclick操作将不会执行
<a href="@Url.Action("ShowClient", "Client", new { clientCode=item.ClientCode})">
<i class="icon-arrow-right"></i>
</a>
查询字符串参数应该是clientCode
,而不是id
,因为在action方法中有参数名clientCode
还有一个建议:您需要删除表单,因为这里您没有提交任何内容。您的视图的目的只是显示记录并导航到特定的ClientCode
并添加一个链接,用于创建使用锚定的链接您需要使用锚定标记而不是按钮 实际上,表单中有一个按钮,因此当您单击按钮时,它将提交表单,而您的onclick操作将不会执行
<a href="@Url.Action("ShowClient", "Client", new { clientCode=item.ClientCode})">
<i class="icon-arrow-right"></i>
</a>
查询字符串参数应该是clientCode
,而不是id
,因为在action方法中有参数名clientCode
还有一个建议:您需要删除表单,因为这里您没有提交任何内容。您的视图的目的只是显示记录并导航到特定的ClientCode
并添加一个用于使用锚创建的链接只需使用
@Html.ActionLink()
:
对于更复杂的类型,例如添加自定义图像或图标,可以使用
只需使用@Html.ActionLink()
:
对于更复杂的类型,例如添加自定义图像或图标,可以使用
或添加type=“button”
属性,我假设或添加type=“button”
属性,我假设
@Html.ActionLink("ShowClient", "ShowClient", "Client", new { id=item.ClientCode }, new { @class = "btn btn-primary btn-sm" })