2014-12-02 5 views
0

У меня есть сетка Кендо, первый столбец - гиперссылка. Каждая гиперссылка привязана к имени файла PDF. Файл PDF будет находиться в папке проекта.MVC Kendo Grid Ссылка на действие

Сетка:

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

Теперь вэнь я щелкаю по ссылке «file1.pdf» Я хочу это имя файла передается в модель. Также эта ссылка действия должна иметь действие «GetPdf», которое вернет PDFResult. Как только эта ссылка будет нажата, соответствующий файл pdf должен быть открыт на одной странице в теге <object>. так же, если я нажму вторую ссылку, тогда file2.pdf следует открыть внутри тега объекта с помощью outpostback. Как это достичь? вот мой код. Мой Вид:

@(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"); 
     } 

Datasource связать сетку:

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> 

ответ

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); 


    } 
Смежные вопросы