2017-04-04 26 views
0

Web開發新手。 我有一個允許用戶選擇一個excel文件的視圖。 當提交「預覽」按鈕時,文件被讀取並且數據被髮回給用戶以預覽數據。 然後,我希望能夠將模型發送回數據庫上傳控件。 (這是我正在努力的部分)。如何在asp.net中將複雜模型傳遞迴控制器mvc

視圖模型:

public class UploadItemsViewModel 
{ 
    public List<Item> Items { get; set; } 

    public int CompanyID { get; set; } 
    public Company Company { get; set; } 

    public HttpPostedFileBase upload { get; set; } 

    public UploadJournalsViewModel() 
    { 
     Items = new List<Item>(); 
    } 

} 

控制器:

public ActionResult Upload(FormCollection formCollection, int CompanyID) 
    { 
     if (Request != null) 
     { 
      HttpPostedFileBase file = Request.Files["UploadedFile"]; 
      if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) 
      { 
       string fileName = file.FileName; 
       string fileContentType = file.ContentType; 
       byte[] fileBytes = new byte[file.ContentLength]; 
       var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); 
      } 
     } 
     UploadItemsViewModel itmViewModel = new UploadItemsViewModel { Company = db.Companies.Find(CompanyID), CompanyID = CompanyID }; 
     return View(itmViewModel); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Upload(UploadItemsViewModel itmViewModel, string Preview, string Upload) 
    { 
     if (ModelState.IsValid) 
     { 
      if (itmViewModel.upload != null && itmViewModel.upload.ContentLength >0) 
      { 
       try 
       { 
        itmlViewModel.Items = App.Services.ItemsMassUploadFileRead.ReadExcelFile(itmViewModel.upload, db.Companies.Find(itmViewModel.CompanyID)); 

        if (string.IsNullOrEmpty(Preview)) 
        { 
         foreach (var itm in itmViewModel.Items) 
         { 
          itm.StartDate = DateTime.Today; 
          itm.CompanyID = itmViewModel.CompanyID; 
          itm.User = null; 
          itm.Items.Add(itm); 
          db.SaveChanges(); 
         } 
         return View(); 
        } 
        else 
        { 
         return View(itmViewModel); 
        } 

        }     } 
       catch (Exception ex) 
       { 
        ModelState.AddModelError("File", ex.Message.ToString()); 
        return View(itmViewModel); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("File", "Please Upload Your file"); 
      } 
     } 
     return View(itmViewModel); 
    } 

檢視:

@using (Html.BeginForm("Upload", "ItemsUpload", null, FormMethod.Post, new { enctype = "multipart/form-data" })) 

{@ Html.AntiForgeryToken() @ Html.HiddenFor(型號=> model.CompanyID )

<div class="form-group"> 
    <div class="input-group"> 
     <label class="input-group-btn"> 
      <span class="btn btn-default"> 
       Browse&hellip; <input type="file" style="display: none;" accept=".xlsx" name="upload"> 
      </span> 
     </label> 
     <input type="text" class="form-control " readonly> 
    </div> 
    <span class="help-block"> 
     Please use a provided Excel template 
    </span> 
</div> 
<div class="form-group"> 
    <input type="submit" value="Preview" name ="Preview" class="btn btn-default" disabled style="display: none" id="submit"/> 
</div> 
<div class="form-group"> 
    <input type="submit" value="Upload" name="Upload" class="btn btn-default" id="Upload" /> 
</div> 

<div class="help-block" id="previewHelp" style="display: none"> 
    Preview results and scroll down to upload data to the database. 
</div> 



if (Model.Journals.Count != 0) 
{ 
    table here to preview the upload 
} 

點擊上傳按鈕模型後,沒有「items」集合。

+1

你要渲染要張貼回的所有數據''元素呈現在視圖中的任何輸入。請參閱http://stackoverflow.com/q/16321736/1450855關於如何正確地列出POST列表,以便MVC模型綁定器取消它們。 –

+0

謝謝。使用這個管理解決問題。 – GlutVonSmark

回答

2

Items名單將始終在控制器null,因爲你不使用名稱Items

相關問題