Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Button element with parameters

Tags:

c#

asp.net-mvc

How do I pass parameters to a controller action method with a button element?

This is not a form submit, I just want a button that is going to take you to another page, but the action method requires parameters.

I currently have:

<button type="button" class="btn btn-info" onclick='window.location = "@Url.Action("Index", "ReviewPendingApprovals", new object[] { Model.QuoteGuid })";'>

However, this results in empty HTML:

<button type="button" class="btn btn-info" onclick='window.location = "";'>

what exactly am I doing wrong here?

like image 502
Patrick Avatar asked Oct 18 '25 13:10

Patrick


2 Answers

It's best to use an HTML helper for this.

@Html.ActionLink(
  "ButtonText", 
  "Index",                         // controller action
  "ReviewPendingApprovals",        // controller
  new { Model.QuoteGuid },         // action parameters aka route values
  new { @class = "btn btn-info" }) // html attributes
like image 198
JB06 Avatar answered Oct 20 '25 03:10

JB06


RIGHT ANSWER HERE: You did two things wrong: you didn't mark the onClick delegate as an href, and you didn't map the argument/route values to any parameter (but that didn't cause the render HTML markup to be blank)

I prefer to use input tags, but you can use a button tag

Here's what your solution should look like using HTML:

< input type="button" class="btn btn-info" onclick='window.location.**href** = "@Url.Action("Index", "ReviewPendingApprovals", new { **someParamHere** = Model.QuoteGuid })";'/>
like image 28
Guest Avatar answered Oct 20 '25 04:10

Guest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!