0

私はこの技術に不慣れで、アプリケーションにインポートした Excel のリストを渡すのに問題があります。コードは次のとおりです。

問題は、Create Controller のモデルが null になるため、データベースに保存できないことです。

データベースに保存する前に値を編集するつもりなので、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();
        }
    }

これは私の見解です。ここに何か問題があるに違いありません

@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 コントローラーに送信したいので、アップロード完了ビューをレンダリングできます (リストは null になります)。そのアクション UploadComplete ビューでレンダリングするモデルを作成コントローラーに送信して、データをデータベースに保存できるようにします。そして、前に言ったように、このデータをアップロード完了ビューで編集するつもりなので、インデックス アクションでデータベースに保存することはできません。

よろしくお願いします。

4

1 に答える 1

0

あなたのコードでわかるように:

  • [HttpPost] 属性はありません
  • フォームアクションはGET
  • 不適切なページ レンダリング (非表示の要素)

このを見てください。POST でリストをバインドする方法を示します。

于 2012-09-20T21:26:21.593 に答える