1

非常に明白な何かが欠けているか、これまで読んだ内容を理解していないと確信しています。データのテーブルを含む from があり、そのうちの 2 つのフィールドを編集可能にする必要があります。フォームが受け取るデータはIEnumerable. ただし、コントローラ関数が を受信する代わりにポスト データを受信するIEnumerableと、何も取得しません。生のデータ型だけを受け取ると、正しい id フィールドを持つオブジェクトの単一のインスタンスが取得され、他のすべてのフィールドは空になります。誰かが私を正しい方向に向けてもらえますか?

MODEL: (最初に EF モデルによって生成されます)

Partial Public Class QuoteBundlePackage_Result
    Public Property id As Integer
    Public Property employeeId As Nullable(Of Integer)
    Public Property employeeName As String
    Property bundleId As Nullable(Of Integer)
    Public Property bundleDescription As String
    Public Property packageId As Nullable(Of Integer)
    Public Property packageContents As String
End Class

意見:

@ModelType IEnumerable(Of gbip_new.QuoteBundlePackage_Result)

@Using Html.BeginForm()
@Html.ValidationSummary(True)
<fieldset>      
<legend>Quote</legend>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(Function(model) model.employeeId)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.employeeName)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.bundleId)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.bundleDescription)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.packageId)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.packageContents)
        </th>
        <th></th>
    </tr>

    @For Each item In Model
        Dim currentItem = item
        Html.HiddenFor(Function(modelItem) currentItem.id)
        @<tr>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.employeeId)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.employeeName)
            </td>
            <td>
                @Html.EditorFor(Function(modelItem) currentItem.bundleId)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.bundleDescription)
            </td>
            <td>
                @Html.EditorFor(Function(modelItem) currentItem.packageId)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.packageContents)
            </td>
        </tr>
    Next
</table>
<p>            
    <input id="Submit1" type="submit" value="submit" />
</p>
</fieldset>
End Using

コントローラ

    <HttpPost()> _
    Function QuoteBundlePackage(ByVal eqDetails As IEnumerable(Of Global.gbip_new.QuoteBundlePackage_Result)) As ActionResult
        If ModelState.IsValid Then

            'Do stuff

            Return RedirectToAction("Index")
        End If

        Return View(eqDetails)
    End Function
4

1 に答える 1

0

コレクションへのモデルのバインドは少し異なります。コレクションを正しくバインドする場合は、ループしている各アイテムに効果的に番号を付ける必要があります。

レンダリングしたいのは...

<input type="text" name="QuoteBundlePackage_Result[0].EmployeeID" value="" />
<input type="text" name="QuoteBundlePackage_Result[0].EmployeeName" value="" />

<input type="text" name="QuoteBundlePackage_Result[1].EmployeeID" value="" />
<input type="text" name="QuoteBundlePackage_Result[1].EmployeeName" value="" />

...これにより、フレームワークが各アイテムを区別できるようになります。この配置を作成するには、ループ内で各アイテムにIDを指定する必要があります-(回答がvbではなくc#であるため、事前に申し訳ありません!)

@for (int i = 0; i < Model.Count(); i++)
{
    @Html.EditorFor(e => e[i].EmployeeID)
    @Html.EditorFor(e => e[i].EmployeeName)
}

ScottHanslemanPhilHaackの関連記事を参照してください。

于 2012-09-29T00:05:19.920 に答える