Url.Action没有击中控制器方法

时间:2018-05-22 05:01:51

标签: c# get url.action

我有一个表,其中包含指向控制器中方法的链接。 该表位于带有提交按钮的表单中。

以下是我的观点:

    @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方法没有被点击。 知道我可能做错了什么吗? 感谢

2 个答案:

答案 0 :(得分:2)

只需使用public class test extends AsyncTask<String,Void,Integer>{ int a; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Integer doInBackground(String... params) { String value1 = params[0]; String value2 = params[1]; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("a", Integer.valueof(value1)); request.addProperty("b", Integer.valueof(value2)); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 60 * 10000); androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive soapPrimitive = (SoapPrimitive) envelope.getResponse(); a = new Integer(Integer.valueOf(soapPrimitive.toString())); }catch (Exception e){ } return a; } @Override protected void onPostExecute(Integer integer) { super.onPostExecute(integer); Toast.makeText(MeetActivity.this, ""+String.valueOf(integer), Toast.LENGTH_SHORT).show(); } }

@Html.ActionLink()

对于更复杂的类型,例如添加自定义图片或图标,您可以使用@Html.ActionLink("ShowClient", "ShowClient", "Client", new { id=item.ClientCode }, new { @class = "btn btn-primary btn-sm" }) 标记,或者作为示例,您可以使用Font Awesome's directional icons

<强> DotNetFiddle Example

答案 1 :(得分:1)

您需要使用锚标记而不是按钮

实际上您在表单中有按钮,因此当您单击按钮时它将提交表单并且您的onclick操作将无法执行,

       <a href="@Url.Action("ShowClient", "Client", new { clientCode=item.ClientCode})">
               <i class="icon-arrow-right"></i>
       </a>

和查询字符串参数应为clientCode而不是id,因为您在操作方法中使用clientCode的参数名称

还有一个建议:您需要删除表单,因为在这里您没有提交任何内容,您的视图的目的只是显示记录并导航为ClientCode

使用锚点

添加创建链接