普段オフショアでコーディングお願いしているのですが、先方さんが旧お正月のため二週間近くお休み。。。
しばらく自力で頑張らないといけません。
C#というか、.net もなんとなく、なんとなーく使っているので、いつまでたってもビギナーのまま。。
IEnumerable<>がControllerのパラメータで取れない
まず、View
@model IEnumerable<site.Models.PetModel>
@using (Html.BeginForm("Index", "Master", FormMethod.Post, new { @id = "frmIndex", role = "form" }))
{
<button type="button" data-request-url="@Url.Action("Save")">SAVE</button>
<table>
<thead>
<!-- 中略 -->
</thead>
<tbody>
@foreach (site.Models.PetModel itm in Model)
{
<tr>
<td>@Html.HiddenFor(m => itm.display_no)</td>
<td>@Html.HiddenFor(m => itm.name)</td>
<td>@Html.CheckBoxFor(m => itm.bEnabled)</td>
<!-- 中略 -->
</tr>
}
</tbody>
</table>
}
ViewのModelを
@model IEnumerable<site.Models.PetModel>
こんな風に書いたら
[HttpPost]
public ActionResult Save(IEnumerable<PetModel> model)
{
}
public ActionResult Save(IEnumerable<PetModel> model)
これで取れるんじゃないの?と思ったら取れなかった。
海外サイトにいろんなやり方書いてあったけど、どれもうまくゆかず・・。
Request.Form.Get()を使う
public ActionResult Save()
{
string[] enabled = Request.Form.Get("itm.bEnabled").Split(',');
}
結局、Request.Form.Get()でとれたよ。いいのかな?
もっとスマートなやり方ないでしょうか?
・・・あるかもしれない。