【发布时间】:2012-09-20 21:09:27
【问题描述】:
我是这项技术的新手,我在传递导入到我的应用程序的 Excel 列表时遇到了一些问题,代码如下:
问题是创建控制器中的模型为空,所以我无法保存到数据库中。
我之前无法在上传完成操作中保存它,因为我打算在保存到数据库之前编辑值。
[HttpPost]
public ActionResult Index(HttpPostedFileBase excelFile)
{
if (excelFile != null)
{
//Save the uploaded file to the disc.
string savedFileName = Server.MapPath("~/UploadedExcelDocuments/" + excelFile.FileName);
excelFileHandler.ImportExcel(savedFileName, excelFile);
return RedirecToAction("UploadComplete",excelFileHandler.DataToEdit);
}
else { return RedirectToAction("Error", "Upload"); }
}
public ActionResult UploadComplete(List<Persona> DataToEdit) // This comes out null so i cant render the view now
{
return View();
}
[HttpPost]
public ActionResult UploadComplete(IEnumerable<ExcelImport.Persona> model)
{
return View();
}
public ActionResult Create(IEnumerable<ExcelImport.Models.Person> model)
{
using (ExcelimportDBTestEntities context = new ExcelimportDBTestEntities())
{
foreach (ExcelImport.Models.Person person in model)
{
Persona newPerson = new Person();
newPersona.Id = person.Id;
newPersona.Birthday= persona.Birthday;
newPersona.LastName= persona.LastName;
newPersona.Name = persona.Name;
context.Persons.AddObject(newPersona);
context.SaveChanges();
}
return View();
}
}
这是我的观点,这里肯定有问题
@model IEnumerable<ExcelImport.Models.Person>
@{
ViewBag.Title = "UploadComplete";
}
<h2>UploadComplete</h2>
@Html.BeginForm(){
<table>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Last Name
</th>
<th>
Birthday
</th>
<th>
Options
</th>
</tr>
@foreach (var item in Model) {
@Html.HiddenFor(model => item)
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Birthday)
</td>
<td>
</td>
</tr>
}
</table>
<input type="submit" value="Upload!"/>
}
编辑:昨天我很累,所以我放了一些……让我们来做一下我错误地做的“测试”,现在这就是我真正想做的。我有一个上传文件并发送到帖子索引控制器的索引视图,从那里我想将列表发送到我的 UploadComplete 控制器,所以我可以渲染 UploadComplete 视图(列表为空),并在帖子中我想将我在 UploadComplete 视图中渲染的模型发送到我的创建控制器,这样我就可以将我的数据存储到数据库中。正如我之前所说,我无法将其保存到索引操作中的数据库中,因为我打算在 uploadcomplete 视图中编辑这些数据。
提前致谢,问候。
【问题讨论】:
-
我很困惑。您如何上传任何内容,您的所有数据仅显示。为什么你还有表格? DataToEdit 是 ExcelImport.Models.Person 的集合吗?你说这是创建表格?但标题是 UploadComplete?
-
我编辑帖子,看看写了什么。
标签: asp.net-mvc-3 razor