I'm having a problem using the HiddenFor and Hidden helpers to store data to be POSTed back to a Controller. I've got a Controller with two methods:
Function Index() As ActionResult
Dim model As New FormPOSTViewModel With {.Name = "Test", .Description = "Test description goes here", .Value = 12}
Return View(model)
End Function
<HttpPost>
Function Update(model As FormPOSTViewModel) As ActionResult
Dim updated As New FormPOSTViewModel
updated.Name = model.Name & "_x"
updated.Description = model.Description & "_x"
updated.Value = model.Value * 2
Return View("Index", updated)
End Function
and a view with three forms, one using HTML, one using HiddenFor and one using Hidden:
@ModelType MVCAjaxWorkbench.FormPOSTViewModel
@Using Html.BeginForm("Update", "FormPOST")
@:<table>
@:<tr><td>@Html.DisplayFor(Function(m) Model.Name)</td></tr>
@:<tr><td><input type="hidden" id="Name" name="Name" value="@Model.Name" /></td></tr>
@:<tr><td><input type="hidden" id="Description" name="Description" value="@Model.Description" /></td></tr>
@:<tr><td><input type="hidden" id="Value" name="Value" value="@Model.Value" /></td></tr>
@:</table>
@:<input type="submit" />
End Using
@Using Html.BeginForm("Update", "FormPOST")
@:<table>
@:<tr><td>@Html.DisplayFor(Function(m) Model.Name)</td></tr>
@:<tr><td>@Html.Hidden("Name", Model.Name)</td></tr>
@:<tr><td>@Html.Hidden("Description", Model.Description)</td></tr>
@:<tr><td>@Html.Hidden("Value", Model.Value)</td></tr>
@:</table>
@:<input type="submit" />
End Using
@Using Html.BeginForm("Update", "FormPOST")
@:<table>
@:<tr><td>@Html.DisplayFor(Function(m) Model.Name)</td></tr>
@:<tr><td>@Html.HiddenFor(Function(m) Model.Name)</td></tr>
@:<tr><td>@Html.HiddenFor(Function(m) Model.Description)</td></tr>
@:<tr><td>@Html.HiddenFor(Function(m) Model.Value)</td></tr>
@:</table>
@:<input type="submit" />
End Using
and a ViewModel:
Public Class FormPOSTViewModel
Public Property Name As String
Public Property Description As String
Public Property Value As Integer
End Class
What I want to happen is that the model is updated each time the Submit button is pressed appending '_x' to the end multiple times so that it ends up something like 'Test_x_x_x_x_x_x'. If I use the Submit button in the form where I've created the HTML by hand then everything works okay.
However what actually happens when I fire this up and press one of the other Submit buttons is that only a single '_x' is ever appended. This seems to be because the model that the Update method receives on the second and subsequent times around is the model sent to the page by the original Index method.
Is this bug with Hidden/HiddenFor or am I using them incorrectly?