新規にリストの値をバインドするなら問題ないのですよ。
Model
- Public Class PrivateInfo
- Public Name As String
- Public History As List(Of History)
- End Class
- Public Class History
- Public eventDate As DateTime
- Public Score As Int32
- Public Cost As Int32
- End Class
View
- <%
- For each item In Model.History
- Dim index As Integer = Model.History.IndexOf(item)
- %>
- <%=Html.TextBox(String.Format("History[{0}].Score", index), item.Score)%>
- <%=Html.TextBox(String.Format("History[{0}].Cost", index), item.Cost)%>
- <% Next%>
ところがですね、すでにある程度の値が入っているリストに画面でeventDateを設定させようとしてUpdateModelすると、ScoreとCostが消えちゃうのです。
View
- <%
- For each item In Model.History
- Dim index As Integer = Model.History.IndexOf(item)
- %>
- <%=Html.TextBox(String.Format("History[{0}].eventDate", index), item.eventDate)%>
- <% Next%>
Controller
- <AcceptVerbs(HttpVerbs.Post)> _
- Public Function Input2(ByVal collection As FormCollection) As ActionResult
- Dim model As Models.PrivateInfo = Session("PrivateInfo")
- UpdateModel(model)
- Return View(model)
- End Function
そこまでやってくれないんですね。
対策としては、その他の項目もPostしちゃうと。
View
- <%
- For each item In Model.History
- Dim index As Integer = Model.History.IndexOf(item)
- %>
- <%=Html.TextBox(String.Format("History[{0}].eventDate", index), item.eventDate)%>
- <%=Html.Hidden(String.Format("History[{0}].Score", index), item.Score)%>
- <%=Html.Hidden(String.Format("History[{0}].Cost", index), item.Cost)%>
- <% Next%>
イケテナイ。
自分でマージするか
0 件のコメント:
コメントを投稿