Hello gurus,
I have a view to create a user as follows:
<% using (Html.BeginForm("SaveUser", "Security")) {%>
<p>
<label for="UserName">UserName:</label>
<%= Html.TextBox("UserName") %>
<%= Html.ValidationMessage("UserName", "*") %>
</p>
<p>
<label for="Password">Password:</label>
<%= Html.TextBox("Password") %>
<%= Html.ValidationMessage("Password", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
<}%>
When the "Create" button is clicked, the HTML form is posted to an action called "SaveUser" that accepts "POST" verb only as follows:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveUser(UserViewModel user)
{
//user.Id is zero before save
//Save the user. Code omitted...
//user.Id is now greater than zero
//redirect to edit user view
return View("EditUser", user );
}
After the user is saved, the page is redirected to the "EditUser" view with
<p>
<label for="Id">Id:</label>
<%= Html.Hidden("Id", Model.Id)%>
</p>
Here is the problem: the value for the hidden field kept showing up as zero though Model.Id is greater than zero. It seemed that something else is overriding the model view value. ViewDataDictonary was a suspect. So a line is added before returning the view in the action as follows:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveUser(UserViewModel user)
{
//user.Id is zero before save
//Save the user. Code omitted...
//user.Id is now greater than zero
//clear the view data
ViewData = new ViewDataDictionary();
//redirect to edit user view
return View("EditUser", user );
}
Sure enough, this worked. The hidden field now has a value of the correct user id.
We found a way to treat the sympotum but: Where is the source of the problem?
I don't like the idea of clearing view data dictionary every time before a returning another view.
If any guru could enlighten me, I'd be most grateful?
Thanks,
Cullen