Search code examples
c#geturl.action

Url.Action not hitting controller method


I have a table containing links to a method in a controller. The table is found in a form with a submit button.

Below is my view:

    @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>
}

My ShowClient method in ClientController:

 public ActionResult ShowClient(string clientCode)
    {
        if (clientCode == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        *some other actions*
    }

When I click on the Create New button this is working correctly. But when I click on the Url.Action in the table my ShowClient method is not being hit. Any idea what I may be doing wrong pls? Thanks


Solution

  • You need to use anchor tag instead of button,

    Actually you have button inside the form so when you click on button it will submit the form and your onclick action will not perform,

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

    and query string parameter should be clientCode instead of id as you have param name with clientCode in action method

    One more suggestion: You need to remove form because here you are not submitting any thing the purpose of your view is to just show records and navigate for perticular ClientCode,

    And add a link for create using anchor