3
<%:ViewData["galleryId"]%>
<% using (Html.BeginForm(
             "FinishEdit" , 
             "GalleryManager" , 
             FormMethod.Post , 
             new { enctype = "multipart/form-data" }
             )
         ) 
   {%>
    <%:Html.Hidden("galleryId" , ViewData["galleryId"])%>
<% } %>

The view data outside of the form renders correctly, but the viewdata inside the form does not. What is going on?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Collin O'Connor
  • 1,351
  • 4
  • 19
  • 31

2 Answers2

2

Html.Hidden helper looks first ModelState dictionary. This could be a reason.

Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118
2

Try clearing the model state in your controller action if you intend to modify any of the POSTed variables and render the same view:

[HttpPost]
public ActionResult FinishEdit()
{
    ...
    ModelState.Remove("galleryId");
    ViewData["galleryId"] = "some new gallery id";
    return View();
}

Html helpers are first looking in the model state dictionary values before ViewData and Model.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928