【问题标题】:mvc form not posting table datemvc表单不发布表格日期
【发布时间】:2013-02-28 19:16:57
【问题描述】:

我确定我遗漏了一些非常明显的东西,或者我不理解我目前所读到的内容。我有一个包含数据表的 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

【问题讨论】:

    标签: asp.net-mvc razor


    【解决方案1】:

    模型绑定到集合的工作方式略有不同。如果您希望正确绑定您的集合,则需要有效地对循环中的每个项目进行编号。

    您要渲染的内容类似于...

    <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 - (抱歉,答案是在 c# 中而不是 vb 中!)

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

    参见Scott HanslemanPhil Haack的相关文章。

    【讨论】:

      猜你喜欢
      • 2011-01-15
      • 2021-11-29
      • 2017-01-18
      • 2023-04-09
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多