2017-04-04 88 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

相关问题