【问题标题】:ASP.Net MVC Hidden field on form postback表单回发上的 ASP.Net MVC 隐藏字段
【发布时间】:2013-04-08 19:08:46
【问题描述】:

我的解决方案中有一个控制器和一个强类型视图。在视图中,我有一个最初为空白的隐藏字段。我在视图中有一个表单,用户可以单击“提交”按钮将表单提交给控制器操作。在控制器操作中,我正在修改模型中的一个值,然后使用修改后的模型重新显示相同的表单(之前发布的)。在视图上,我在 HTML 隐藏字段标记内写入模型字段的值,以便客户端 javascript 可以看到它。我遇到的问题是,在 javascript 中,回发表单上隐藏字段的值是空白的,即使在回发后正确设置了隐藏字段的服务器端值。我需要的是客户端 javascipt 能够看到隐藏字段的修改值。我需要做些什么才能做到这一点?

<HttpPost()> _
Function Index(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 = ""
  Dim vrl As List(Of MaxServerLib.ValidationResult) = Nothing

  Try
    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]"

      ' First thing we do is to perform valiation of the criteria
      model.ValidateFieldValues(sCriteria)
      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

        ' Populate the message to be displayed to the user
        model.FormatErrorMessage()
        ' test code start
        ModelState.Clear()
        ' test cod end
        ' Return RedirectToAction("Index", New With {.sMaxUrl = model.MaxUrl, .sDataSource = model.DataSource, .sSessionTicket = model.SessionTicket, .dtLastCall = model.LastCall, .sFileCabinetid = model.FileCabinetId, .sFileCabinetName = model.FileCabinetName, .sShowMsg = MaxServerLib.EscapeString(model.ShowMsg)})
        Return View(model)
      Else
        ' All field values are valid, now attempt to add the document
        If model.ExportDocument() Then
          ' Document export was successful
          System.Diagnostics.Debugger.Break()
        Else
          ' Document export failed for some reason
          ModelState.AddModelError("", model.LastError)
          Return View(model)
        End If
      End If

      ' Return RedirectToAction("Index", "SearchResults", New With {.sMaxUrl = model.MaxUrl, .sDataSource = model.DataSource, .sSessionTicket = model.SessionTicket, .dtLastCall = model.LastCall, .sFileCabinetId = model.FileCabinetId, .sFileCabinetName = model.FileCabinetName, .sSearchCriteria = sSearchCriteria})
    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

【问题讨论】:

  • 显示隐藏字段相关的视图标记
  • 几乎不可能在没有看到一些标记的情况下提供帮助,Brian。
  • 查看标记到隐藏字段是:@跨度>
  • 在上面,我可以放置一个断点并从服务器端看到隐藏字段设置正确,但客户端 java-script 没有读取更新的值。

标签: asp.net-mvc forms hidden-field


【解决方案1】:

如果您要回发相同的表单,则很可能是在绑定时从模型状态中提取原始值。

在返回表单之前调用 ModelState.Clear() 以查看是否可以解决问题。

【讨论】:

  • 我已经尝试清除模型状态,但没有解决问题。此问题显然与视图中的 javascript 代码何时执行有关。我曾假设 javascript 只会在服务器端代码完成后执行,但如果服务器端代码正确设置隐藏字段但客户端代码没有看到更新的值,这可能不是真的。跨度>
  • 当部分返回时,如何将其返回到您的页面中?你能编辑你的问题并在那里显示代码吗?
  • 我将修改后的模型传递给视图。请参阅处理帖子的控制器代码的已编辑问题。
猜你喜欢
  • 1970-01-01
  • 2014-03-29
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多