0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[ASP.NET Core] HTMLヘルパーを使ってリスト形式の入力画面を作るときの注意

Posted at

#HTMLヘルパーを入力画面かつList型のクラスで使うときの注意
ASP.NET Core (MVC)でHTMLヘルパーを使って、テーブルタグで複数行入力し、List型のクラスでポストデータを受け取る画面を作ろうとしたら、入力データがポストされずハマったのでメモ

結論はforeachじゃなくて普通のforでやればOK

NG

foreachだとpostされてこないよ

@foreach (var row in Model.Rows)
{
<tr>
    <td>@Html.EditorFor(m => row.ColA)</td>
    <td>@Html.EditorFor(m => row.ColB)</td>
</tr>
}

OK

forだとOK

@for(var i = 0; i < Model.Rows.Count; i++)
{
<tr>
    <td>@Html.EditorFor(m => m.Rows[i].ColA)</td>
    <td>@Html.EditorFor(m => m.Rows[i].ColB)</td>
</tr>
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?