如何在asp.net mvc中将复杂模型传递回控制器

时间:2017-04-04 13:35:07

标签: asp.net-mvc

Web开发新手。 我有一个允许用户选择excel文件的视图。 提交"预览"按下按钮文件被读取并且数据被发送回用户以预览数据。 然后我希望能够将模型发送回数据库上传的控件。 (这是我挣扎的部分)。

视图模型:

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 =&gt; 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
}

单击“上传”按钮后,模型将返回,而不会显示&#34;项目&#34;集合。

1 个答案:

答案 0 :(得分:2)

Items列表在控制器中始终为null,因为您未在视图上呈现名称为Items的任何输入

相关问题