コントローラに返す必要のあるデータの行がいくつかあるテーブルがあります。私の見解では、最初に期間を選択してボタンをクリックすることでテーブルをロードします。テーブルには関連するすべてのレコードが読み込まれますが、テーブルセルの1つにドロップダウンリストが含まれています。したがって、ドロップダウンで[更新]をクリックして選択できるはずです。コントローラーが変更を保存します。
だから私が保存しようとするまですべてが機能します。コントローラに送信されるモデルは完全にnullです。テーブルセルに関連付けたリストプロパティは、コントローラーnullに戻ります。
@ModelType SuperViewModel
//We need this a view model in order to store a List of the models in the table
@Using (Html.BeginForm())
@For Each i in Model.CompleteList
Dim currentItem = i //MVC auto-generated extra declarations. Seems redundant to me but it works.
@<table>
<tr>
<td>@Html.DisplayFor(function(Model)currentItem.Name)</td>
<td>@Html.DisplayFor(function(Model)currentItem.SampleTime)</td>
<td>@Html.DropDownListFor(function(Model)currentItem.WorkTime, ViewBag.WorkTimeList)</td>
</tr>
Next
</table>
<input name="submit" type="submit" value="Update"/>
End Using
//Controller
<HttpPost()>
function Save(vmodel as SuperViewModel, submit as String) as ActionResult //NOTE: submit parameter is used because we have two submit buttons but its not relevant here
if submit = "Update"
db.Entry(vmodel.CompleteList).State = EntityState.Modified//Here the exception is throw because our list is null at this point even tho its tied to the model in the view.
db.SaveChanges()
end if
End Function
注:これはVB.NETで記述されていますが、C#ヘルプは大歓迎です。私はMVCの両方の言語に精通しています。