mvc form not posting table date

2019-08-05 04:19发布

I am sure I am missing something very obvious, or I am not understanding what I have read so far. I have a from that contains a table of data, 2 fields of which need to be editable. The data received by the form is IEnumerable. However, when the controller function receiving the post data, instead of it receiving an IEnumerable, I get nothing. If I receive just the raw data type, I get the a single instance of the object with the correct id field and all other fields are empty. Could someone please point me oin the right direction?

MODEL: (Generated by EF Model first)

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

View:

@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

Controller

    <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

1条回答
forever°为你锁心
2楼-- · 2019-08-05 05:00

Model binding to a collection works a little differently. You need to effectively number each item you are looping through if you want your collection to be bound correctly.

What you want to render is something like...

<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="" />

... which will allow the framework to distinguish between each item. To create this arrangement you should give each item an ID within your loop - (Sorry in advance for answer being in c# and not vb!)

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

See related articles from Scott Hansleman and Phil Haack.

查看更多
登录 后发表回答