Share via


MVC5: How to pass parameter values to button click

Question

Wednesday, September 10, 2014 12:29 PM

I have a button inside the grid. When user clicks on the button i need to call a controller action by passing the value of ID column. The ID column is also a part of the grid

The following syntax doesn't work

<button class="approve-button" 
            onclick="location.href='@Url.Action("Approve", "Batch", new { id= #: ID # } )'">Approve</button>

All replies (7)

Wednesday, September 10, 2014 3:28 PM âś…Answered

Why don't you try:

onclick="location.href='@Url.Action("ApproveBatchDetail", "BatchDetail")?batchID=#: BatchID #&batchDetailID=#: BatchFileID #'

Wednesday, September 10, 2014 12:53 PM

Change your button to....

@Html.ActionLink("Approve", "Approve", "Batch", new { id = Your_ID_val }, new { @class = "btn" })

note: must be using bootstrap for the "btn" class to change actionlink into button.  Alternative if no bootstrap:

<button>@Html.ActionLink("Approve", "Approve", "Batch", new { id = You_ID_val }, null)</button>

Wednesday, September 10, 2014 12:57 PM

new { id = Model.ID } maybe?

Although since you mention this is in a grid it's probably going to be different. It might be: new { id = item.ID } perhaps, if it's within a foreach loop.\WHat I'm trying to say is that it would be good to see more of your page.


Wednesday, September 10, 2014 1:02 PM

i am looking for a razor syntax to read the value of ID.

For example the link below is also one of the column in the Grid and this syntax works for the link

 <a href="/Api/FileDownload/Download?fid=#: ID #" target="_blank">#: FileName #</a>

The link is invoking API controller while button will invoke Controller

What would be the syntax for button click. 


Wednesday, September 10, 2014 1:12 PM

Are you using the WebGrid helper?  You would manually code your button into the column

grid.Column("Button", format: @<text>
                    @Html.ActionLink("Approve", "Approve", "Batch", new { id = @item.ID }, new { @class = "btn" })
         </text>)

Wednesday, September 10, 2014 1:18 PM

The grid is Kendo UI Grid :). Also the collection that is bound to the grid is not part of the Model


Wednesday, September 10, 2014 4:20 PM

So.. what doesn't work about it? What happens when you use your original button syntax? Does it give a compiler error? Does it render the wrong url? Show us the actual output and what you expect the output to look like.