How to get ViewData inside a form to display correctly?
<%:ViewData["galleryId"]%>
<% using (Html.BeginForm(
"FinishEdit" ,
"GalleryManager" ,
FormMethod.Post ,
new { enctype = "multipart/form-data" }
)
)
{%>
<%:Html.Hidden("galleryId" , ViewData["galleryId"])%>
<% } %>
View data outside the form is displayed correctly, but not viewdatainside the form. What's happening?
Try to clear the state of the model in the action of your controller if you intend to change any of the POSTED variables and display the same view:
[HttpPost]
public ActionResult FinishEdit()
{
...
ModelState.Remove("galleryId");
ViewData["galleryId"] = "some new gallery id";
return View();
}
Html helpers primarily look for the values of the model state dictionary before ViewData and Model.
The Html.Hidden helper first looks like a ModelState dictionary. This may be the reason.