2017-09-15 48 views
0

我有一個web應用程序中,其具有以下功能:從MS Excel的如何在asp中渲染控制器數據。淨視圖

  1. 提取數據文件
  2. 進程中的數據並將其存儲在基於特定標準的數據結構。
  3. 將數據結構從控制器傳遞到它們可以呈現的視圖。

我遇到的問題出現在前幾個步驟。在我的視圖頁面上,我有一個按鈕,用戶可以上傳excel文件。一旦他們點擊提交,發送POST請求將文件發送到控制器操作(我正在使用索引操作,但我不確定是否正確),其中的數據是從文件中提取的。處理完文件後,我想要將提取的數據顯示在與上載按鈕相同的頁面上。

我試圖通過在控制器中創建一個類來爲每個excel行實例化,然後每行存儲在三個不同的對象列表之一中。

我然後存儲每個列表中的ViewBag對象:

//Handle POST request and determine in the file uploaded was of correct type 

List<Dictionary<string, string>> dictionary = new List<Dictionary<string, string>>(); 
      bool isSuccess = true; 
      int colID = 0; 
      int colTier = 0; 
      if (Request != null) 
      { 
       HttpPostedFileBase file = Request.Files["UploadedFile"]; 

       if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) 
       { 
        string fileName = ""; 
        //string fileinitPath = "//app235wnd1t/equityfrontoffice/INGDS/TierBulkUpload/"; 
        string fileinitPath = "C:/Users/chawtho/Desktop/"; 
        Regex regex = new Regex("WTDA.+xlsx"); //find correct filename 
    if (match.Success) 
       { 
        fileName = (match.Value); 
       } 
       if (fileName != "") 
       { 

       Match match = regex.Match(file.FileName); 
       //Extract data from excel file and store in collections 
    ViewBag.inactive_subscriptions = inactiveSubscriptions; 
    ViewBag.active_subscriptions = activeSubscriptions; 
    } 
    return View(); 
    } 

在視圖中我有以下幾點:

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title></title> 
</head> 
<body> 
    random text 


    @using (Html.BeginForm("Index", "Subscription", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
    <fieldset class="form"> 
     <legend>Upload Document</legend> 
     <input type="file" name="UploadedFile" id="FileUpload" required /> 
     <input type="submit" name="Submit" value="Upload" /> 
     <label id="saveStatus" style="color: red"> 
     </label> 
    </fieldset> 
    } 

    @{ 
     <li>@ViewBag.inactive_subscriptions[0].ID</li> 
    } 

在這裏,我只是想讀取ID字段的訂閱列表中的第一個對象,但我得到的錯誤:

Cannot perform runtime binding on a null reference 

我不知道這個錯誤在哪裏com因爲當我調試控制器代碼時,Viewbag將在返回View()之前填充兩個列表。我也嘗試將Subscription類從控制器移動到模型類,並創建一個容器來容納訂閱列表,但這並沒有解決問題。

我認爲這個問題可能與這樣一個事實有關,即在頁面最初加載時打印viewbag數據的代碼存在,但我不確定是否應該保留該文件以防止其運行處理。

我應該如何構建這個mvc設置來實現我所概述的內容?

+0

你可以張貼的控制器代碼? ,我認爲問題就像你所說的,當列表沒有記錄是異常的原因時,你試圖訪問索引[0]。解決這個問題的最好方法是將方法中的後處理數據返回,例如在Json對象中。 –

+0

你確定ViewBag.inactive_subscriptions不是空的列表嗎? 我會添加檢查,如果ViewBag.inactive_subscriptions!= null和ViewBag.inactive_subscriptions.Count> 0 – Munzer

+0

@FernandoAguilar我剛剛更新了更多的控制器代碼的帖子。當你說退還處理的數據,你是說它應該被髮送到JSON對象的視圖?看起來,我可能需要某種方式讓viewbag在文件處理完畢並且數據已被髮回後才顯示數據。 – loremIpsum1771

回答

1

我把下一個例子,這是我用來操縱excel文件的方式,注意我沒有使用viewbag變量,這可能是一個選項,就像我說過的,我返回處理過的數據到一個JSON對象,然後通過JavaScript來操縱它。

- 剃刀的ViewPage

<!--Upload File--> 
@using (Html.BeginForm("ProcessExcelFile", "ControllerName", FormMethod.Post, 
    new { id = "formUploadExcel", enctype = "multipart/form-data" })) 
{ 
    <div class="row"> 
     <div class="col-md-4"> 
      <label for="file">Excel File (.xls, .xlsx)</label> 
      <input type="file" name="file" id="file" required="required"> 

     </div> 
    </div> 
    <br> 
    <button type="submit" class="btn btn-primary">Upload File</button> 
} 

- JS腳本

<script type="text/javascript"> 
    $('form#formUploadExcel').unbind('submit').bind('submit', function() { 
     formdata = new FormData($('form#formUploadExcel').get(0)); 
     $.ajax({ 
      url: this.action, 
      type: this.method, 
      cache: false, 
      processData: false, 
      contentType: false, 
      data: formdata, 
      success: function (data, status) { 
       console.log(data); 
      }, 
      complete: function() { 
       //code... 
      } 
     }); 
     return false; 
    }); 
</script> 

- 控制器

[HttpPost] 
public JsonResult ProcessExcelFile(HttpPostedFileBase file) 
{ 
    // Process the excel... 
    var business = new BusinessLayer(); 
    var data = business.ValidateExcel(file); 

    // Return the Json with the procced excel data. 
    return Json(data, JsonRequestBehavior.AllowGet); 
} 
+0

感謝您的答案!它看起來像空引用錯誤消失了,當我在視圖中添加檢查。我現在遇到的問題是,當數據被髮送回客戶端時,控制器似乎試圖調用相同的索引視圖導致服務器錯誤。用你的解決方案,是否應該有一個不同的視圖,將處理後的數據發送回去?另外,我需要使用XML,但我假設它可以替代JSON。 – loremIpsum1771

+0

不,我將處理後的數據返回到相同的視圖,當我做AJAX調用時,這就是爲什麼我在AJAX中設置了返回false,從而阻止重新加載頁面。 –

+0

@Resource在哪裏定義? – loremIpsum1771

相關問題