Here are the facts of my issue:
- I have a form in an ASP.Net MVC 4 web application.
- The fields on the form are not tied to explicit static properties within the model. Instead they are dynamic fields that can change.
- When the form is submitted, I use a FormcCollection to retrieve the values entered into the field values.
- The first time I submit the form, all is well: The values of the FormCollection accurately reflect the form values.
- If there is a problem with the field values due to one or more invalid fields, I redisplay the form.
- If the user changes the values of the form to correct the validation errors and then resubmits the same form, the FormCollection is NOT updated to reflect the latest form values. Instead it always contains the field values from the first submit.
Why is this happening and how can I correct it?
<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
EDIT:
What appears to be happening is that the browser has cached the post action from the first post, and on every subsequent post (after the first) it renders the cached results of the first post instead of rendering the results of the current post. Why would it be doing this?