【问题标题】:How to pass a complex model back to the controller in asp.net mvc如何将复杂模型传递回asp.net mvc中的控制器
【发布时间】:2017-04-04 13:35:07
【问题描述】:

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 => 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
}

单击“上传”按钮模型返回后没有“项目”集合。

【问题讨论】:

  • 您必须为要回发的所有数据呈现&lt;input/&gt; 元素。请参阅stackoverflow.com/q/16321736/1450855,了解如何正确发布列表,以便 MVC 模型绑定器理解它们。
  • 谢谢。设法解决了这个问题。

标签: asp.net-mvc


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多