2013-04-08 83 views
0

我在我的解決方案中有一個控制器和一個強類型的視圖。在視圖中,我有一個最初空白的隱藏字段。我在視圖中有一個表單,用戶可以單擊「提交」按鈕將表單提交給控制器操作。在控制器操作中,我正在修改模型內的值,然後使用修改後的模型重新顯示相同的表單(以前發佈的表單)。在視圖上,我將模型字段的值寫入HTML隱藏字段標記內,以便客戶端javascript可以看到它。我遇到的問題是,在javascript中,即使隱藏字段的服務器端值在回發後正確設置,發佈的表單上的隱藏字段的值也爲空。我需要的是客戶端javascipt能夠看到隱藏字段的修改值。我需要做些什麼才能做到這一點?ASP.Net MVC表單回傳中的隱藏字段

<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 
+0

顯示與隱藏字段相關的視圖標記 – Igor 2013-04-08 19:11:24

+0

幾乎不可能在沒有看到某些標記的情況下提供幫助,Brian。 – 2013-04-08 20:56:50

+0

查看隱藏字段的標記是:@ 2013-04-10 13:48:07

回答

0

如果您發回相同的表單,那麼最有可能在綁定時將原始值從模型狀態拉回。

在返回表單之前調用ModelState.Clear()以查看是否修復它。

+0

我已經嘗試清除模型狀態,但它沒有解決問題。這個問題顯然與視圖中的JavaScript代碼執行時相關。我曾假設只有在服務器端代碼完成之後纔會執行javascript,但如果服務器端代碼正確設置隱藏字段,但客戶端代碼沒有看到更新的值,則這可能不是真的。 – 2013-04-10 13:45:42

+0

當部分返回時,如何將其返回到您的頁面?你能編輯你的問題並在那裏顯示代碼嗎? – Slicksim 2013-04-10 13:58:31

+0

我將修改後的模型傳遞給視圖。請參閱處理帖子的控制器代碼的編輯問題。 – 2013-04-11 20:40:30