title: MVC form data loss after validation
body: <p>Ok, let me start out by saying that I am brand new to MVC. With that out of the way, I am using the version of MVC that ship with Visual Studio 2010. This is also using Visual Basic, but I am sure that you can figure that out once you see the code.</p>
  
  <p>I am trying to validate my data, and the validation process is working correctly. However, if the validation fails the values that were in my textbox are not reloaded on the form. </p>
  
  <p>My form is fairly simplistic as I wanted to start with something simple for my first application. It consists of a table that is tied to a collection of Agreement objects. At the bottom of the table, I have two textboxes for adding a new agreement. It is these two textboxes that are not getting the data posted back to them.</p>
  
  <p>Here is the view:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Food.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of NdaToolLibrary.BusinessLayer.Agreement))" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="PrimaryContent" runat="server"&gt;
  
  &lt;h1&gt;Agreement List&lt;/h1&gt;
  
  &lt;table cellpadding="0" cellspacing="0" border="0" class="tableDefault" style="width: 300px !important"&gt;
      &lt;tr&gt;
          &lt;th&gt;
              AccountType
          &lt;/th&gt;
          &lt;th&gt;
              NationalId
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  &lt;% For i As Integer = 0 To Model.Count - 1%&gt;
  
      &lt;tr class="&lt;%= IIF(i mod 2 = 0, "", "detailRow") %&gt;"&gt;
          &lt;td&gt;
              &lt;%: Model(i).AccountType%&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;%: Model(i).NationalId%&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;%: Html.RouteLink("Delete", "Delete", New With {.action="Delete", .acctType = Model(i).AccountType, .id = Model(i).NationalId})%&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  
  &lt;% Next%&gt;
  
      &lt;tr class="footerRow"&gt;
          &lt;td&gt;
              &lt;%= Html.TextBox("AccountType")%&gt;
              &lt;%= Html.ValidationMessage("AccountType", " ")%&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;%= Html.TextBox("NationalId")%&gt;
              &lt;%= Html.ValidationMessage("NationalId"," ")%&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;a href="javascript: document.forms[0].action='/Agreement/Create'; document.forms[0].submit();"&gt;Create&lt;/a&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;%= Html.ValidationSummary("Could not save agreement. Reasons include:", New with{.class = "red"})%&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Here is the controller:</p>
  
  <pre><code>Namespace Web
  Public Class AgreementController
      Inherits System.Web.Mvc.Controller
  
      Public Function Create(ByVal FormValues As FormCollection) As ActionResult
          Dim agreement As New BusinessLayer.Agreement()
  
          agreement.AccountType = FormValues("AccountType").ToString()
          agreement.NationalId = FormValues("NationalId").ToString()
  
          If agreement.IsValid Then
              Try
                  agreement.Save()
              Catch ex As Exception
                  ModelState.AddModelError("", ex.Message)
              End Try
          Else
              For Each validationError As BusinessLayer.DataValidationMessage In agreement.GetValidationResults()
                  ModelState.AddModelError(validationError.Property, validationError.Message)
              Next
          End If
  
          'Try
          '    agreement.AccountType = FormValues("AccountType").ToString()
          '    agreement.NationalId = FormValues("NationalId").ToString()
  
          '    agreement.Save()
          'Catch dvEx As BusinessLayer.DataValidationException
          '    For Each validationError As BusinessLayer.DataValidationMessage In dvEx.ValidationMessages
          '        ModelState.AddModelError(validationError.Property, validationError.Message)
          '    Next
          'Catch ex As Exception
          '    ModelState.AddModelError("", ex.Message)
          'End Try
  
          Dim agreements As New BusinessLayer.AgreementCollection()
          agreements.Load()
  
          Return View("Index", agreements)
      End Function
  
      Public Function Delete(ByVal AcctType As String, ByVal Id As String) As ActionResult
          Dim agreement As New BusinessLayer.Agreement()
          agreement.AccountType = AcctType
          agreement.NationalId = Id
  
          Return View("Delete", agreement)
      End Function
  
      &lt;AcceptVerbs(HttpVerbs.Post)&gt; _
      Public Function Delete(ByVal AcctType As String, ByVal Id As String, ByVal FormValues As FormCollection) As ActionResult
          Dim agreement As New BusinessLayer.Agreement(AcctType, Id)
          agreement.Delete()
  
          Return RedirectToAction("Index")
      End Function
  
      Public Function Index() As ActionResult
          Dim agreements As New BusinessLayer.AgreementCollection()
          agreements.Load()
  
          ' Return View("Index", agreements) is implied because the view has the same name as the action
          Return View(agreements)
      End Function
  
  
  
  End Class
  End Namespace
  </code></pre>