簡體   English   中英

網址操作未達到控制器方法

[英]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的方向性圖標

DotNetFiddle示例

您需要使用定位標記而不是按鈕,

實際上,您在表單中有按鈕,因此當您單擊按鈕時,它將提交表單,並且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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM