将视图渲染到客户端后的StackOverflowException

时间:2014-09-23 15:07:51

标签: entity-framework asp.net-mvc-5 entity-framework-6

我只是在学习MVC如果这是一个新手错误而道歉,但我无法在其他任何地方找到解决方案。

我正在尝试创建一个可以编辑多个记录的页面。在调试模式下,我的视图将在我的浏览器中呈现和显示,但随后我在System.Web.dll中获得了一个StackOverflowException。如果我将代码发布到生产服务器,则HTTP请求永远不会完成(超时)。

这是带有EntityFramework 6.1的MVC5。

这是控制器的动作方法:

' GET: HonorBoxes
Function Index() As ActionResult
    Dim hb = From h In db.honorBoxes Select h Where Not h.Filled And Not h.Hold
    Return View(hb.ToList())
End Function

观点:

@ModelType IEnumerable(Of Vending.HonorBox)
@Code
ViewData("Title") = "Index"
End Code

<h2>Index</h2>

@Html.BeginForm("Index", "HonorBoxes")
@Html.AntiForgeryToken()
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(Function(model) model.BoxID)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.Qty)
        </th>
    </tr>

@For x = 0 To Model.Count - 1
    @code 
        Dim item As HonorBox = Model(x)
    End Code
    @<tr>
        <td>
            @Html.Display(item.BoxID)
            @Html.Hidden("boxes(" & x & ").BoxID", item.BoxID)
        <td>
            @Html.TextBox("boxes(" & x & ").Qty", item.Qty)

        </td>
    </tr>
Next

</table>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</div>

最后,相关的模型类(如果它是相关的):

Public Class HonorBox
    Private _filled As Nullable(Of Boolean)
    Private _qty As Nullable(Of Integer)
    Private _hold As Nullable(Of Boolean)


    <Key> Public Property BoxID As Integer
    Public Property AssetID As Nullable(Of Integer)
    Public Property Asset As Asset
    Public Property BoxType As String
    Public Property Hold As Nullable(Of Boolean)
        Get
            If IsDBNull(_hold) Then Return False
            Return _hold
        End Get
        Set(value As Nullable(Of Boolean))
            _hold = value
        End Set
    End Property
    Public Property Filled As Nullable(Of Boolean)
        Get
            If IsDBNull(_filled) Then Return False
            Return _filled
        End Get
        Set(value As Nullable(Of Boolean))
            _filled = value
        End Set
    End Property
    Public Property Qty As Nullable(Of Integer)
        Get
            If IsDBNull(_qty) Then Return 0
            Return _qty
        End Get
        Set(value As Nullable(Of Integer))
            _qty = value
        End Set
    End Property

End Class

编辑 - 因此有时会从序列化库中抛出异常。这让我想知道是否是我试图编辑的导致StackOverflowException的记录数。我减少了显示的记录数量,确定它运行正常。我不确定是什么导致这种情况 - 如果可能,我仍然希望一次编辑更多记录。

编辑#2(增加的视图模式) - 直到下面提到它们之前我才意识到视图模型。我在项目中添加了一个HonorBoxView模型(只有.Qty和.BoxID属性),更改了View中的模型类型,并将Controller方法修改为以下内容:

    Function Index() As ActionResult
        Dim hb = From h In db.honorBoxes Select h Where Not h.Filled And Not h.Hold
        Dim hbv As IEnumerable(Of HonorBoxView) = From h In hb Select New HonorBoxView() With {.Qty = h.Qty, .BoxID = h.BoxID}
        Return View(hbv.ToList())
    End Function

    <HttpPost>
    Function Index(boxes As List(Of HonorBoxView)) As ActionResult
        If ModelState.IsValid Then
            For Each box In boxes
                Dim cbox = db.honorBoxes.Find(box.BoxID)
                If Not IsDBNull(box.Qty) AndAlso cbox.Qty <> box.Qty Then
                    cbox.Qty = box.Qty
                    cbox.Filled = True
                End If
            Next
            db.SaveChanges()
        End If
        Return RedirectToAction("Index")
    End Function

我仍然得到相同的StackOverflowException。它在将HTML呈现给客户端之后但在请求完成之前发生。

0 个答案:

没有答案