<% using (Html.BeginForm( "FinishEdit" , "GalleryManager" , ...">

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?

+3
source share
2 answers

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.

+2
source

The Html.Hidden helper first looks like a ModelState dictionary. This may be the reason.

+2
source

Source: https://habr.com/ru/post/1794601/


All Articles