MVC Kendo Grid Action Link

时间:2014-12-02 12:27:49

标签: asp.net-mvc-4 kendo-ui asp.net-mvc-5 kendo-grid

我有一个Kendo Grid,第一列是超链接。每个超链接被绑定到一个pdf文件名.Pdf文件将在项目文件夹中。

网格:

  File Name          Date
----------------|--------
     file1.pdf  |12.03.2014
     file2.pdf  |13.03.2014   

现在我点击此链接'file1.pdf'我希望将此文件名传递给模型。此Action链接也应该有一个动作'GetPdf',它将返回PDFResult。单击此链接后,相应的pdf文件应在<object>标记的同一页面中打开。同样地,如果我点击第二个链接,那么file2.pdf应该在没有pagepostback的对象标签内打开。怎么实现这个?这是我的代码。 我的观点:

 @(Html.Kendo().Grid<Myproject.Models.PdffilesModel>()
        .Name("grid")
        .Columns(columns =>
        {
columns.Bound(p => p.FileName)
                              .ClientTemplate(
        "<a  href='" +
            @Html.ActionLink("#=FileName#'", "GetPdf", "Home")+
            "/#= FileName #" +
        "</a>" );
 columns.Bound(c => c.CreatedDate).Width(70);
 })

        .HtmlAttributes(new { style = "height: 350px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(1))
        .DataSource(dataSource => dataSource
            .Ajax()
                      .ServerOperation(false)

            .Read(read => read.Action("Customers_Read", "Home"))
        )
)

HomeController://这里我对文件名进行了硬编码,但它应该来自超链接点击操作

public FileStreamResult GetPdf(string filenmae)
        {
            FileStream fs = new FileStream("/pdfSample.pdf", FileMode.Open, FileAccess.Read);
            return File(fs, "application/pdf");
        }

绑定网格的数据源:

public ActionResult Customers_Read([DataSourceRequest]DataSourceRequest request)
        {
            return Json(GetAttachments().ToDataSourceResult(request));
        }

        private static IEnumerable<PdffilesModel>GetAttachments()
        {

            IEnumerable<PdffilesModel> finalresult ;
            List<PdffilesModel> list= new List<PdffilesModel>();
            var northwind = new PdffilesModel();

            northwind.FileName = "file1.pdf";           
            northwind.CreatedDate = new DateTime(2014,03,04).ToString("d");
            list.Add(northwind);           

            finalresult = list;

            return finalresult;
        }

当点击网格操作链接时,这是对象标签应与pdf结果绑定。

<object class="pdfdiv" id="ajaxpdf" data="@Url.Action("GetPdf")"></object>

1 个答案:

答案 0 :(得分:0)

********网格********************* ******

 @(Html.Kendo().Grid<SearchEmailHistoryGridModel>()
            .Name("EmailSearchGrid")
            .Columns(columns =>
            {

                columns.Bound(p => p.ExtractId).Visible(false);
                columns.Bound(p => p.FileName).Title("File Name");
                columns.Bound(p => p.TemplateName).Title("Template");
                columns.Bound(p => p.CentreName).Title("Centre");
                columns.Bound(p => p.LeaseId).Title("Lease ID");
                columns.Bound(p => p.Unit).Title("Unit").Width("80px");
                columns.Bound(p => p.Occupant).Title("Occupant");
                columns.Bound(p => p.ContactName).Title("Contact");
                columns.Bound(p => p.Attachment).Title("Attachment").ClientTemplate("# if(data.Attachment!=null) {# <div style='cursor: hand;'><a class='gridLink' href='\\#' id='slpitLink'  title='Download #:Attachment#' onclick=\"DownloadFile(#:ExtractId#,'#:FileName#','#:Attachment#'); return false;\">View</a></div> #}#");
                columns.Bound(p => p.EmailAddress).Title("Email").Width("150px");
                columns.Bound(p => p.EmailSent).Title("Email Sent").Visible(false);
                columns.Bound(p => p.FirstSentDate).Title("Date Sent").Width("85px");
                columns.Bound(p => p.SentBy).Title("Sent By").Width("85px");
                columns.Bound(p => p.Delivered).Title("Delivered").Width("85px");
                columns.Bound(p => p.Read).Title("Read").Width("70px");
                columns.Bound(p => p.Exception).Title("Error").ClientTemplate("# if(data.Exception=='Yes') {# <a style='color:\\#94BB27; text-decoration:none; cursor:help' title='#:data.ExceptionDescription#'>Yes</a> #} else {# <label>No</label>  #}#").Width("60px");



                columns.Command(command =>
                {
                    command.Custom("Resend").Click("ResendEmail").HtmlAttributes(new { title = "Resend Email" });
                    command.Custom("Forward").Click("SendForwardEmail").HtmlAttributes(new { title = "Forward Email" });
                }).Title("Action");


            })
            .Events(e => e.DataBound("onEmailSearchDataBound"))
            .Pageable(pageable => pageable
                    .Refresh(true)
                    .PageSizes(true)
                    .PageSizes(new int[] { 5, 10, 20,50 })
                    .ButtonCount(5)
                    )
            .Filterable()                
            .Scrollable()
            .HtmlAttributes(new { style = "height:650px;" })
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Events(events => events.Error("error_handler"))  
                .Model(model =>
                    {
                        model.Id(c => c.ExtractId);

                    })   
                .Read(read => read.Action("ReadEmailHistory", "EmailManagement").Data("GetSearchFilterData")) 

            )

        )

**********脚本*************** ********************

  //Download File
          function DownloadFile(extractId,folder,fileNameParam) {


              $.post('@Url.Action("DownloadFile", "Correspondence")', { extractId: extractId, folder: folder, fileName: fileNameParam })
                .done(function (data) {
                    if (data.url == null) {

                        showMessage(data);
                    }
                    else {

                        $("body").append("<iframe src='" + data.url + "' style='display: none;'></iframe>");
                    }

                });

          }

**********************************控制器************ ****************

   //Download File
    [HttpPost]
    [FilterSessionExpire]
    public ActionResult DownloadFile(int extractId,string folder, string fileName)
    {

        var filePath = Path.Combine(ConfigurationManager.AppSettings["EcorsSplitFilePath"],folder, fileName); //in folder level

        if (System.IO.File.Exists(filePath))
        {
            //return file link
            return Json(new { url = Url.Action("DownloadFileActual", "Correspondence", new { folder = folder, fileName = fileName }) }, JsonRequestBehavior.AllowGet);
        }

        //else return error
        return Json("File:" + fileName + " Not found.", JsonRequestBehavior.AllowGet);
    }


   //Actual Download File 
    public ActionResult DownloadFileActual(string folder, string fileName)
    {
        const string contentType = "application/pdf";
        var filePath = Path.Combine(ConfigurationManager.AppSettings["EcorsSplitFilePath"], folder, fileName); //in folder level
        return File(filePath, contentType, fileName);


    }