0

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>
Serge
  • 40,935
  • 4
  • 18
  • 45
Char
  • 146
  • 6

1 Answers1

1

try this

  <td class="options">
 @using(Html.BeginForm("RedirectToSettings","Settings"))
         {
           <input type="hidden"  value="@item.LocationId" />
           <input type="submit" value="Settings" />
         }
  <td class="options">
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Thank you Serge, the link now takes me to an error page saying that locationId is null, which is most likely due to the value not being set in your solution. I tried changing it to value="@{ new { locationId = item.LocationId } }, but that also didn't work. – Char Nov 15 '21 at 13:24
  • I figured it out! If I move the routevalues into the BeginForm declaration it works. Thank you again Serge for pointing me in the right direction. – Char Nov 15 '21 at 13:32