以下是我的问题的事实:
- 我在 ASP.Net MVC 4 Web 应用程序中有一个表单。
- 表单上的字段与模型中的显式静态属性无关。相反,它们是可以更改的动态字段。
- 提交表单时,我使用 FormcCollection 检索输入到字段值中的值。
- 我第一次提交表单时,一切都很好:FormCollection 的值准确地反映了表单值。
- 如果由于一个或多个无效字段而导致字段值出现问题,我会重新显示表单。
- 如果用户更改表单的值以更正验证错误,然后重新提交相同的表单,则不会更新 FormCollection 以反映最新的表单值。相反,它始终包含第一次提交的字段值。
为什么会发生这种情况,我该如何纠正?
<HttpPost()> _
<HttpParamAction()> _
Function Upload(ByVal model As MaxDocument, formcollection As FormCollection) As ActionResult
Dim sCriteria As String = ""
Dim nKeyIndex As Integer = 0
Dim nFieldIndex As Integer = -1
Dim sFieldValue As String = ""
Try
' Build sCriteria from the submitted formcollection
model.GetFileCabinetFieldList()
For nFieldIndex = 0 To (model.IndexFieldCount - 1)
sFieldValue = ""
If nFieldIndex > 0 Then
sCriteria += "~"
End If
Dim fcf As MaxServerLib.FileCabinetField = model.criterionAtIndex(nFieldIndex)
' Get the field value corresponding to this field
For Each oKey As Object In formcollection.AllKeys
If oKey.ToString = fcf.sFieldName Then
sFieldValue = formcollection(oKey.ToString)
Exit For
End If
Next
sCriteria += sFieldValue
Next
If sCriteria = "" Then sCriteria = "[BlankIndex]"
' Set the criteria property of the model, which will be used for both field validation and document export.
model.Criteria = sCriteria
' First thing we do is to perform valiation of the criteria
model.ValidateFieldValues()
If Not model.AllFieldValuesValid() Then
' Handle case where one or more field values are invalid.
' In this case we want to redisplay the form but show an error message listing the invalid fields
model.HasAttemptedUpload = True
' Set tempData before redirect:
TempData("MaxDocument") = model
Return RedirectToAction("Index")
Else
' All field values are valid, now attempt to add the document
...
End If
Catch ex As Exception
System.Diagnostics.Debugger.Break()
End Try
'End If
' If we got this far, something failed, redisplay form
Return View(model)
End Function
编辑:
似乎正在发生的事情是浏览器已经缓存了第一个帖子的帖子操作,并且在每个后续帖子(第一个帖子之后)它呈现第一个帖子的缓存结果,而不是呈现当前帖子的结果。为什么会这样做?