Asp.net mvc 3 Net MVC 3-通过不同视图持久化列表

Asp.net mvc 3 Net MVC 3-通过不同视图持久化列表,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我是这项技术的新手,在将导入到应用程序的excel列表传递给我时遇到了一些问题,下面是代码: 问题是Create控制器中的模型为空,因此我无法保存到数据库中 在uploadcomplete操作中,我之前无法保存它,因为我打算在保存到数据库之前编辑这些值 [HttpPost] public ActionResult Index(HttpPostedFileBase excelFile) { if (excelFile != null) {

我是这项技术的新手,在将导入到应用程序的excel列表传递给我时遇到了一些问题,下面是代码:

问题是Create控制器中的模型为空,因此我无法保存到数据库中

在uploadcomplete操作中,我之前无法保存它,因为我打算在保存到数据库之前编辑这些值

 [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();
        }
    }
[HttpPost]
公共操作结果索引(HttpPostedFileBase Excel文件)
{
if(excelFile!=null)
{
//将上载的文件保存到光盘。
字符串savedFileName=Server.MapPath(“~/uploadeExcelDocuments/”+excelFile.FileName);
ImportExcel(savedFileName,excelFile);
返回重定向操作(“UploadComplete”,excelFileHandler.DataToEdit);
}
else{return RedirectToAction(“Error”,“Upload”);}
}
public ActionResult UploadComplete(List DataToEdit)//结果为空,因此我现在无法呈现视图
{
返回视图();
}
[HttpPost]
公共操作结果上传完成(IEnumerable模型)
{
返回视图();
}
公共行动结果创建(IEnumerable模型)
{
使用(ExcelImportDBTestities上下文=新建ExcelImportDBTestities())
{
foreach(模型中的ExcelImport.Models.Person)
{
Persona newPerson=新人();
newPersona.Id=person.Id;
newPersona.birth=persona.birth;
newPersona.LastName=persona.LastName;
newPersona.Name=persona.Name;
context.Persons.AddObject(newPersona);
SaveChanges();
}
返回视图();
}
}
这是我的观点,这里一定出了什么问题

@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!"/>
  }
@model IEnumerable
@{
ViewBag.Title=“上传完成”;
}
上传完成
@Html.BeginForm(){
身份证件
名称
姓
生日
选择权
@foreach(模型中的变量项){
@Html.HiddenFor(model=>item)
@DisplayFor(modeleItem=>item.Id)
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.LastName)
@DisplayFor(modeleItem=>item.Birthday)
}
}
编辑:我昨天很累,所以我放了一些。。。让我们去“测试”,我是错误地做的,现在这是我真正想做的。我得到了一个上传文件并发送到post Index控制器的索引视图,从那里我想将列表发送到我的UploadComplete控制器,这样我就可以呈现UploadComplete视图(列表为空),在该操作的post中,我想将我在UploadComplete视图中呈现的模型发送到我的create控制器,这样我就可以将数据存储到数据库中。正如我之前所说,我无法将其保存到索引操作中的数据库中,因为我打算在uploadcomplete视图中编辑这些数据


提前感谢,谢谢。

从您的代码中可以看出:

  • 没有[HttpPost]属性
  • 行动的形式是获得
  • 页面呈现不正确(隐藏元素)

看看这个。它显示了如何将列表绑定到POST上。

我很困惑。你是如何上传任何东西的,你所有的数据都只是显示而已。你为什么有表格?DataToEdit是ExcelImport.Models.Person的集合吗?你说这是一个创建表单?但是标题是UploadComplete?我编辑了文章,检查写的内容。@user1687202
返回重定向操作(“UploadComplete”,excelFileHandler.DataToEdit)只是浏览器中的302重定向,在类似转换为超链接的情况下。查看
TempData
。谢谢!这就是我一直在寻找的。有一个问题,我必须将索引中的列表传递给上传控制器并呈现它,但是我如何才能将lets say final列表传递给上传后控制器,结果为空(与视图中的代码相同)@user1687202 simple
foreach
在这种情况下不起作用,因为它会创建错误的名称。创建带有隐藏输入的
DisplayTemplate
,然后binder将填充您的模型。我给你的链接展示了如何做到这一点。当你说隐藏字段时,它类似于Html.HiddenFor(model=>item.id)?如果是这样,我真的不明白如何组合我的模型,请记住,我没有数据库中的数据。我需要foreach来显示我的表,这样我就可以在客户端显示和编辑它。。。