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?

4

2 に答える 2

2

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

于 2011-02-25T06:02:07.057 に答える
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.

于 2011-02-25T07:26:15.770 に答える