【问题标题】:Asp.Net MVC 3 - Persist a List through different ViewsAsp.Net MVC 3 - 通过不同的视图保留列表
【发布时间】: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


【解决方案1】:

正如我在您的代码中看到的那样:

  • 没有 [HttpPost] 属性
  • 表单操作是 GET
  • 页面呈现不正确(隐藏元素)

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

【讨论】:

  • @user1687202 return RedirecToAction("UploadComplete",excelFileHandler.DataToEdit); 只是浏览器中的 302 重定向,在 this 这样的情况下它转换为超链接。看TempData。
  • 谢谢!这就是我想要的。
  • 有一个问题,我必须将索引中的列表传递给上传控制器并渲染它,但是我如何将最终列表传递给上传控制器,结果为空(是视图中的相同代码)
  • @user1687202 simple foreach 在这种情况下不起作用,因为它会创建错误的名称。创建带有隐藏输入的DisplayTemplate,然后活页夹将填充您的模型。我给你的链接显示了它是如何完成的。
  • 当您说隐藏字段时,它类似于 Html.HiddenFor(model => item.id)?如果是我真的不明白如何将我的模型放在一起,请记住我没有数据库中的数据。我需要 foreach 来显示我的表格,这样我就可以在客户端显示和编辑它......
猜你喜欢
  • 2012-09-04
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多