2014-12-04 60 views
0

這裏是kendo網格我有<href>標籤onclick函數()。現在我想傳遞文件名作爲參數到我的onclick函數。我想通過該文件名到CallAjaxMethod函數作爲參數。如何做?如何將參數傳遞給kendo的javascript函數<a href=""></a>

@(Html.Kendo().Grid<MVCAPPModels..AttachmentsModel>() 
     .Name("grid") 
     .Columns(columns => 
     { 
      columns.Bound(p => p.FileName).ClientTemplate("<a id='FileName' href='\\#' data-pdfurl='#= FileName #' onclick='CallAjaxMethod('#= FileName #');'>/#= FileName #</a>"); 

      columns.Bound(c => c.CreatedDate).Width(70); 
      columns.Bound(c => c.CreatedBy).Width(70); 
      columns.Bound(c => c.UpdatedDate).Width(70); 
      columns.Bound(c => c.UpdatedbBy).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")) 
     ) 
) 

JavaScript函數:

function CallAjaxMethod() 
    { 
     alert("page! CallAjax"); 
     $.ajax({ 
      type: "POST", 
      url: "@Url.Action("Submit", "Home")", 
      data: { fileName: "/pdfSample.pdf" }, 
      type: 'GET', 
      cache: false, 
      success: function (result) { 
         $('#partialView_div').data(result); 
      } 
     }); 
    } 

回答

0

首先,參數添加到您的功能,讓您可以實際使用的是什麼傳入:

function CallAjaxMethod(filename) 
{ 
    alert("page! CallAjax"); 
    $.ajax({ 
     type: "POST", 
     url: "@Url.Action("Submit", "Home")", 
     data: { fileName: filename }, 
     type: 'GET', 
     cache: false, 
     success: function (result) { 
        $('#partialView_div').data(result); 
     } 
    }); 
} 

那麼你的HTML和Javascript報價是相互衝突,因爲它們都是單引號,所以你需要改變其中的一些,最好是HTML:

@(Html.Kendo().Grid<MVCAPPModels.AttachmentsModel>() 
     .Name("grid") 
     .Columns(columns => 
     { 
      columns.Bound(p => p.FileName).ClientTemplate("<a id=\"FileName\" href=\"\\#\" data-pdfurl=\"#= FileName #\" onclick=\"CallAjaxMethod('#= FileName #');\">/#= FileName #</a>"); 

      columns.Bound(c => c.CreatedDate).Width(70); 
      columns.Bound(c => c.CreatedBy).Width(70); 
      columns.Bound(c => c.UpdatedDate).Width(70); 
      columns.Bound(c => c.UpdatedbBy).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")) 
     ) 
) 
相關問題