2016-12-28 79 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

即使您已完成检查,我仍然会收到同样的错误 –

相关问题