添加到网格列的超链接

Add a hyperlink to grid column

我想在文件名列上添加一个 link,这样用户就可以单击并打开 PDF 文件(无需下载,只能打开查看)。这是我尝试过的方法,但我不知道如何将文件名传递给服务器函数。

columns.Bound(p => p.FileName)
       .ClientTemplate( "<a href='" + FileHelper.GetFullPath({how to pass the file name here}) + ">/#= FileName #</a>")

FileHelper.GetFullPath 方法是一个服务器函数,用于生成文件的完整路径。完整路径应该是:

http://servername/applicationname/filefolders/filename.pdf

您无法轻松地在客户端实现PDF下载。您应该改为使用另一种操作方法流式传输 PDF 文件。

试试这个:

.ClientTemplate("<a href='" + Url.Action("GetPdf", "Home") + "?fileName=#= FileName #'>#=FileName#</a>");

public ActionResult GetPdf(string fileName)
{
    string path = FileHelper.GetFullPath(fileName);
    FileStream stream = new FileStream(path, FileMode.Open);
    return File(stream, "application/pdf", fileName + ".pdf");
}