コントローラに返す必要があるデータ行がいくつかあるテーブルがあります。私の見解では、まず期間を選択してボタンをクリックしてテーブルをロードします。テーブルに関連するすべてのレコードがロードされますが、テーブルセルの1つにドロップダウンリストが含まれています。だから、ドロップダウンで「更新」をクリックすると、変更が保存されます。MVC:複数行のデータをコントローラに返す
私は試して保存するまですべてが機能します。コントローラに送信されるモデルは完全に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の両方の言語に精通しています。
SuperViewModelはどのように定義されていますか? –
それにはいくつかのプロパティがありますが、ここで適用されるものはList(Of AnotherModel)である "CompleteList"です。その他のモデルは、私たちのDBテーブルに基づいてEntity Frameworkによって生成されました。 – ExceptionLimeCat
'For Each'を閉じるために' End Using'の前に 'Next'がありませんか?また、ビューでレンダリングされたマークアップの一部を追加できますか? –