简体   繁体   English

将 MVC 表单发布到 controller 时为空集合

[英]Empty collection when posting MVC form to controller

I have been at this the last week and could really do with some help.上周我一直在这,真的可以得到一些帮助。

I am submitting a form in MVC that contains a collection.我在 MVC 中提交一个包含集合的表单。 However, the collection is empty (count=0) when it gets returned.但是,该集合在返回时为空(count=0)。 I've tried multiple combinations to get this working.我已经尝试了多种组合来使其正常工作。 I'm using VB.net, but if anyone has a C# solution I won't turn my nose up!我正在使用 VB.net,但如果有人有 C# 解决方案,我不会抬起头来!

Collection Class:集合 Class:

Public Class GridTest
    Inherits System.Collections.CollectionBase

    Public Sub Add(ByVal oGridTestItem As GridTestItem)
        ' Invokes Add method of the List object to add a widget.
        List.Add(oGridTestItem)
    End Sub

    Public Sub Remove(ByVal index As Integer)
        ' Check to see if there is a widget at the supplied index.
        If index > Count - 1 Or index < 0 Then
            ' If no widget exists, a messagebox is shown and the operation is 
            ' cancelled
        Else
            ' Invokes the RemoveAt method of the List object.
            List.RemoveAt(index)
        End If
    End Sub

    Public ReadOnly Property Item(ByVal index As Integer) As GridTestItem
        Get
            ' The appropriate item is retrieved from the List object and 
            ' explicitly cast to the Widget type, then returned to the 
            ' caller.
            Return CType(List.Item(index), GridTestItem)
        End Get
    End Property

    Public Sub load()
        Dim oGTI As GridTestItem

        oGTI = New GridTestItem
        oGTI.Status = "First one"
        Add(oGTI)
        oGTI = Nothing

        oGTI = New GridTestItem
        oGTI.Status = "Second"
        Add(oGTI)
        oGTI = Nothing

    End Sub
End Class

Public Class GridTestItem
    Property Status() As String
        Get
            Return m_strStatus
        End Get
        Set(ByVal Value As String)
            m_strStatus = Value
        End Set
    End Property
    Private m_strStatus As String
End Class

View:看法:

<Table id="grdpsr" class="table table-striped table-hover" style="background-color:white; width:100%">

@Code  Dim intcount = 0  End Code

@For intcount = 0 To Model.GT.Count - 1
    Dim strStatusID As String
    strStatusID = "GT[" & CStr(intcount) & "]_Status"
    @<tr style="line-height:20px;">
        <td width="100px;"><input name="GT[@intcount].Status" id="@strStatusID" style="width:100px; border-color: orange; text-align: right;" value="@Model.GT.Item(intcount).Status" /></td>
    </tr>
Next intcount
</Table>

Controller: Controller:

        Function PleaseWork(e As testViewModel,
                      submit As String) As ActionResult
            
otestViewModel As new testViewModel

            MsgBox(e.ftrCompanyGUID)
            MsgBox(e.GT.Count)          '<-- returns 0
            MsgBox(e.GT.Item(0).Status) '<-- Can't find

            Return View(otestViewModel)
        End Function

The HTML renders okay: HTML 呈现正常:

<Table id="grdpsr" class="table table-striped table-hover" style="background-color:white; width:100%">
    <tr style="line-height:20px;">
        <td width="100px;"><input name="GT[0].Status" id="GT[0]_Status" style="width:100px; border-color: orange; text-align: right;" value="First one" /></td>
    </tr>
    <tr style="line-height:20px;">
        <td width="100px;"><input name="GT[1].Status" id="GT[1]_Status" style="width:100px; border-color: orange; text-align: right;" value="Second" /></td>
    </tr>
</Table>

I'd really appreciate any assistance.我真的很感激任何帮助。

Well, to answer my own question, in case anyone else is interested in this.好吧,回答我自己的问题,以防其他人对此感兴趣。 I could not get this to work, despite spending days working solely on it.尽管花了几天时间单独工作,但我无法让它发挥作用。

What I am doing instead is to create a JSON string containing all of the table data, and post that instead.我正在做的是创建一个包含所有表数据的 JSON 字符串,然后将其发布。 Then I parse the string in the controller and populate the object.然后我解析 controller 中的字符串并填充 object。

It is a real pity I could not just send back the collection object.很遗憾我不能只发回集合 object。 It would have been a far better solution.这将是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM