This is a migrated thread and some comments may be shown as answers.

ClientTemplate on bound column with actionlink with parameters and post

2 Answers 400 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lara
Top achievements
Rank 1
Lara asked on 17 Jul 2013, 11:09 PM
I've read various threads about how to get an actionlink in a column in an AJAX grid and have successfully implemented that portion of it.  However, in the case of this link I need it to use HttpPost to the server.  The purpose of the link is to allow download of a document.  However, we don't want parameters visible in the querystring that identify the particular document as it is in the database.  So I have the Action which is called with the document id via POST retrieve the document, store it in session and return a GUID linked to that session variable along with the URL to the retrieval page.  I then submit a dynamically created form which goes to that retrieval page, passing it the GUID, and gets the document for download.

To that end I created a function to attach to a classed link and use JQuery to post it to the server.  It works perfectly for an ActionLink outside the AJAX bound grid but inside the grid it doesn't call that javascript function, instead it attempts to call the Action via Get which is disallowed so I get a 404 error.  Is there some other way to tell the ActionLink in the grid to use the javascript function? I would appreciate any help you could provide.

I've attached a text file with the javascript function and the ActionLink and Grid declarations.

2 Answers, 1 is accepted

Sort by
0
Accepted
Lara
Top achievements
Rank 1
answered on 19 Jul 2013, 03:16 PM
For anyone interested the solution was to add an onclick htmlattribute to the actionlink.  Have that function return false so it doesn't navigate and don't rely on wiring up the event by class using JQuery.

So for the column:
                  columns.Bound(p => p.DocId).Title("Print").ClientTemplate(@Html.ActionLink("Print", "GetDocument", new { docId= "#=DocId#" }, new { @class = "postLink k-button",onclick="postPrint(this)" }).ToHtmlString());

And in the javascript function do the post
function postPrint(e) {
$.post($(e).attr("href"), function (data) { do whatever you want with the result variable  data here}
return false;
}
0
Daniel
Telerik team
answered on 19 Jul 2013, 07:52 PM
Hello Lara,

The Grid is using Ajax binding and therefore the rows will not be rendered yet on document ready but after the Ajax request is completed. You could either use a delegated handler on the Grid:

$(function () {
        $("#pendingPowersGrid").on("click", "a.postLink", function(e){
             e.preventDefault();
             $.post($(this).attr("href"), function (data) {
             ...
             });
        });
or a custom command. Also, note that the Html.ActionLink helper encodes the route values and the parameter will not be evaluated. You should use the approach demonstrated here for Ajax binding in order to render a link with the evaluated values. Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Lara
Top achievements
Rank 1
Answers by
Lara
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or