【发布时间】:2017-03-02 19:15:47
【问题描述】:
所以我对 ASP.net MVC 很熟悉,但对 C# 并不陌生。我已经在我的项目中链接了数据源,它创建了我所有的 Create Read Update Delete 方法和视图。伟大的。现在我需要自定义一些东西。
首先,我需要上传一个 .csv 文件,这将填充我们的一个模型,作为我们从旧的纸笔系统迁移时的权宜之计。
这是在提交按钮被按下时调用的回发方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "callout_id_pk,start_timestamp,drivers_license_needed,med_training_needed,worksite_code_fk,shift_notes,status,job_class_code_fk,worksite_name,job_class_name,gender_req")] Callout callout)
{
Debug.WriteLine("THIS LINE OF CODE RAN?");
if (ModelState.IsValid)
{
db.Callouts.Add(callout);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
foreach (string upload in Request.Files)
{
if (Request.Files[upload].ContentLength == 0) continue;
string pathToSave = Server.MapPath("~/App_Data/UploadedFiles");
string filename = Path.GetFileName(Request.Files[upload].FileName);
Request.Files[upload].SaveAs(Path.Combine(pathToSave, filename));
}
ViewBag.worksite_code_fk = new SelectList(db.Worksites, "worksite_code_pk", "worksite_name", callout.worksite_code_fk);
return View(callout);
}
这是视图,为了便于阅读,删除了不相关的控件。
@using (Html.BeginForm("Create", "Callouts", FormMethod.Post, new {enctype="multipart/form-data"}))
{
@Html.AntiForgeryToken()
<!--@Html.ValidationSummary(true, "", new { @class = "text-danger" })-->
<div class="form-group">
<input type="file" name="FileUpload1" /><br />
</div>
<!-- other form controls here -->
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
现在,由于某种原因,在回发中,Request.Files 没有看到任何回发的文件。我在这里想念什么?请注意,现在,我只是尝试将文件保存到磁盘。但最终,该文件将被读取并用于填充模型。
【问题讨论】:
标签: c# asp.net .net razor asp.net-mvc-5