Search code examples
asp.net-mvc-2asp.net-mvc-2-validation

How can I make ASP.NET MVC 2 persist submitted form values across multiple submits?


Say I have a strongly-typed view of ViewPage<Song> and some additional fields in the form of create/edit scenarios that are not directly part of the Song entity (because I have to do some parsing before I fetch the composers from a foreign table and create the bridge entities SongComposers).

public ActionResult Create(Song song, string composers) {
  if (ModelState.IsValid) {
    // redirect to details
  }

  // return a view with errors
  return View(song);
}

And in the view

<% using (Html.BeginForm()) { %>
  <%= Html.EditorForModel() %>
  <div class="editor-label"><label for="composers">Composers</label></div>
  <div class="editor-field"><input id="composers" type="text" name="composers" /></div>
<% } %>

Now when I try to create a food and get validation errors, the previously entered form values for fields in Html.EditorForModel get filled properly but the composers field is blank after a submit. How can I fix this (ie. make it so that it "remembers" what the user wrote in the composers field)?


Solution

  • ViewData["composers"] = composers;