Search code examples
c#asp.net-mvcbuttonasp.net-mvc-5actionlink

HTML button from ActionLink in MVC5


I have ActionLinks like so:

    <td class="options">
        @Html.ActionLink("Settings", "RedirectToSettings", new { locationId = item.LocationId })
    </td>

But I want to make it into a button instead of just clickable text. Whether it's making the entire cell clickable or adding in a button element.

I've looked at other questions on SO such as HTML button calling an MVC Controller and Action method, but I wasn't able to figure out how to make it work.

I have tried directly making it a button, and using input:

    <td class="options">
        <input type="button" value="Settings" onclick="@Html.ActionLink("Settings", "RedirectToSettings", new {locationId = item.LocationId})"/>
    </td>

I'm able to get the button, but clicking it doesn't redirect me to the page I specified in "RedirectToSettings" in the controller.

public ActionResult RedirectToSettings(int locationId)
{
    *doing stuff with the locationId*
    return RedirectToAction("StationSettings");
}

Button:

enter image description here

How can I make this work? Any help is greatly appreciated!

Update:

Using Html.BeginForm solves the issue, props to Serge for the idea.

    <td class="options">
        @using (Html.BeginForm("RedirectToSettings", "Configs", new { locationId = item.LocationId }))
        {
            <input type="submit" value="Settings"/>
        }
    </td>

Solution

  • try this

      <td class="options">
     @using(Html.BeginForm("RedirectToSettings","Settings"))
             {
               <input type="hidden"  value="@item.LocationId" />
               <input type="submit" value="Settings" />
             }
      <td class="options">