我試圖下載一個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"))))
}
確保使用eg 'alert(dataItem.possID);'該項目真的產生一個有效的ID。另外值得一試的是使用** [Telerik Fiddler](http://www.telerik.com/fiddler)**來查看從瀏覽器發送到服務器的數據。 –
爲什麼你不測試dataItem.possID是否存在,如果沒有做回報? –
@PeterB我已經提醒它,並得到相同的ID傳遞給控制器 –