Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading