2016-12-28 76 views
0

我試圖下載一個Kendo網格按鈕點擊文件,現在我越來越關注錯誤。我想這就是我無法下載文件的原因。我怎樣才能解決錯誤'參數字典包含一個空參數'possID'的非空類型'System.Int32'

我的參數返回選定行是possId的ID,但它拋出這個錯誤:

The parameters dictionary contains a null entry for parameter 'possID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.FileResult DownloadIndex(Int32)' in 'TTAF.Portal.Parts.Web.Controllers.PossController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter

下載行動

public FileResult DownloadIndex(int possID) 
    { 
     try 
     { 
      OnePossModel md = new Models.OnePossModel(); 
      JsonParamBuilder myBuilder = new JsonParamBuilder(); 
      myBuilder.AddParam<Guid>("submittingUserID", System.Guid.Parse(User.Identity.GetUserId())); 
      myBuilder.AddParam<int>("possID", Convert.ToInt32(possID)); 

      string jsonReq = Models.JsonWrapper.JsonPOST(ApiBaseUrl + ApiPOSSSubBaseUrl + "/WritePOSSFile", myBuilder.GetJSonParam()); 
      string possFilename = Models.DeserialiseFromJson<string>.DeserialiseApiResponse(jsonReq); 

      possFilename = possFilename.Replace(",", ","); 
      var type = System.Net.Mime.MediaTypeNames.Application.Octet; 

      byte[] fileBytes = System.IO.File.ReadAllBytes(Filelocation + possFilename); 
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + possFilename); 
      return File(fileBytes, type, possFilename); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

Javasrcipt

<script type="text/javascript"> 
         function showDetails(e) { 
          e.preventDefault(); 
          var dataItem = this.dataItem($(e.currentTarget).closest("tr")); 
          DownloadIndexController(dataItem.possID); 
         } 
       </script> 


      <script> 
        function DownloadIndexController(possID) { 
         $.ajax({ 
          url: '@Url.Action("DownloadIndex", "Poss")', 
          contentType: 'application/json; charset=utf-8', 
          datatype: 'json', 
          data: { possID: possID }, 
          type: "GET", 
          success: function (data) { 
           window.location = '@Url.Action("DownloadIndex", "Poss")'; 
          } 
         }) 
        } 
      </script> 

我看來

@using (Ajax.BeginForm("SearchPoss", "Poss", Model.possID, new AjaxOptions { HttpMethod = "Post" }, new { @class = "form-horizontal", role = "form"})) 
{ 

    @(Html.Kendo().Grid<Web.Models.OnePossModel.GridPossModels>() 
      .Name("Grid") 
        .Columns(columns => 
        { 
         columns.Command(command => command.Custom("Download").Click("showDetails")).Width(100); 
         columns.Bound(x => x.possID).Title("ID"); 
         columns.Bound(x => x.ordernumber).Title("Order Number"); 
         columns.Bound(x => x.datePublishedBySap).Title("Poss Date").Format("{0:" + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + "}"); 
         columns.Bound(x => x.possType).Title("Poss Type"); 
         columns.Bound(x => x.possSource).Title("Poss Source"); 

        }) 
        .Pageable(pageable => pageable 
        .Refresh(true) 
        .PageSizes(true) 
        .ButtonCount(5)) 
        .Scrollable() 
        .Filterable() 
        .Sortable() 
        .Resizable(resize => resize.Columns(true)) 
        .DataSource(dataSource => dataSource 
        .Ajax() 
        .PageSize(10) 
        .ServerOperation(false) //No post back 
        .Read(read => read.Action("ReadPoss", "Poss")))) 

} 
+0

確保使用eg 'alert(dataItem.possID);'該項目真的產生一個有效的ID。另外值得一試的是使用** [Telerik Fiddler](http://www.telerik.com/fiddler)**來查看從瀏覽器發送到服務器的數據。 –

+0

爲什麼你不測試dataItem.possID是否存在,如果沒有做回報? –

+0

@PeterB我已經提醒它,並得到相同的ID傳遞給控制器​​ –

回答

0

這個問題是自我解釋說DownloadIndex(int possID)需要整型參數的,但不知該參數值是零。當你從你的ajax調用向這個動作方法發出請求時。參數數據:{possID:possID},在action方法中傳遞null值。問題可能在於這段代碼

function showDetails(e) { 
          e.preventDefault(); 
          var dataItem = this.dataItem($(e.currentTarget).closest("tr")); 
          DownloadIndexController(dataItem.possID); 
         } 

其中dataItem.possID未提供任何值。嘗試確保在開發人員工具中dataItem.possId具有價值。如果你確信它可以傳遞null值控制器的操作方法,那麼請給在操作方法默認值,以避免這種異常的喜歡

public FileResult DownloadIndex(int possID = 10OrAnyDegaultNumber) //default value for your possID. 
+0

我已經提醒了數據項,並且我得到了傳遞給控制器​​的相同ID。我的問題是,當我給它的默認值,它如何知道要下載哪個文件,因爲用戶可以從網格中選擇任何行,然後單擊下載按鈕 –

+0

我建議你避免例外,假設如果沒有值傳遞,然後把ID = xxxx並匹配if條件如果id == xxx然後簡單地在網格中顯示一些值「沒有下載內容」,因爲在這種情況下可能無法下載某些腳本,您將再次遇到此類問題。 – Rajput

+0

你可以請我展示我已發佈的代碼,以便我知道方向 –

0

你爲什麼不只是檢查dataItem.possID的價值如果它是空的或不適合你,你退出......這樣的事情:

function showDetails(e) { 
           e.preventDefault(); 
           var dataItem = this.dataItem($(e.currentTarget).closest("tr")); 
if(!dataItem || !dataItem.possID) return; //and maybe other check you want 
           DownloadIndexController(dataItem.possID); 
          } 
+0

即使您已完成檢查,我仍然會收到同樣的錯誤 –

相關問題