I have a mvc project with a page for security question and answer. I need to get the nunmber of wrong answers. I used a hidden control in aspx that is populate from TempData(<%= Html.Hidden("wrongAnswer",TempData["WrongAnswer"]==null?"0":TempData["WrongAnswer"].ToString())
%>).
In my controller action, if I get a wrong answer I use Request["wrongAnswer"] to get value I increment it and put it back to TempData and then return View(). First time I redirect to this action I put in TempData["WrongAnswer"]=0.
The problem is that the hidden always has value 0, my TempData is incrementing correctly but when im submitting my form the hidden doesn't have the correct value. Why is this happening?
Try putting break points in your code and on your .aspx page if you have not already done so.
You've a better chance of seeing what is going on if you patiently step through your .aspx and your code.
Regards,
Gerry (Lowry)
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
P.S.: you might want to post more of your actual code.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
On postback, all input helpers -- including hidden -- render the value that's in ModelState rather than the value that's provided from ViewData or the helper method. The assumption is that if you're re-rendering the form on postback, then it's because there
was an error, and we should show the values the user typed rather than the values in the object.
In your controller action, you could remove the hidden value from ModelState to force it to use the new value.
Marked as answer by Michelee on Oct 02, 2009 07:37 AM
Wouldn't it be simpler to keep track of this number in session state?
This is a question because I do not know the answer and I am unsure whether my understanding is correct.
It seems that the idea of ASP.NET MVC is to be "stateless" and that using "session state" would break that mould.
N'est-ce pas?
Gerry
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
if (String.IsNullOrEmpty(Brad)) // we have to have a Brad ModelState.AddModelError("Brad", "Brad is needed.");
ModelState.Remove("Brad"); // we want a new Brad !!
^^^^^^^^^^^^^^^^^^^^^ PROBLEM: the error message also vanishes.
ONE SOLUTION: clone Brad
if (String.IsNullOrEmpty(Brad)) // we have to have a Brad
ModelState.AddModelError("Brad2", "Brad is needed."); // clone of Brad
ModelState.Remove("Brad"); // we want a new Brad !!
To every solution it sometimes seems there is another challenge to be solved.
Gerry
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
I don't understand... your hidden fields are generating model state errors? How is that possible? Did you generate the hidden field with an invalid value?
Michelee
Member
1 Points
2 Posts
Hidden doesn't get value from TempData
Oct 01, 2009 02:37 PM|LINK
Hi,
I have a mvc project with a page for security question and answer. I need to get the nunmber of wrong answers. I used a hidden control in aspx that is populate from TempData(<%= Html.Hidden("wrongAnswer",TempData["WrongAnswer"]==null?"0":TempData["WrongAnswer"].ToString()) %>).
In my controller action, if I get a wrong answer I use Request["wrongAnswer"] to get value I increment it and put it back to TempData and then return View(). First time I redirect to this action I put in TempData["WrongAnswer"]=0.
The problem is that the hidden always has value 0, my TempData is incrementing correctly but when im submitting my form the hidden doesn't have the correct value. Why is this happening?
hidden control and tempdata
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hidden doesn't get value from TempData
Oct 01, 2009 03:44 PM|LINK
Try putting break points in your code and on your .aspx page if you have not already done so.
You've a better chance of seeing what is going on if you patiently step through your .aspx and your code.
Regards,
Gerry (Lowry)
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hidden doesn't get value from TempData
Oct 01, 2009 03:46 PM|LINK
P.S.: you might want to post more of your actual code.
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Hidden doesn't get value from TempData
Oct 01, 2009 04:36 PM|LINK
On postback, all input helpers -- including hidden -- render the value that's in ModelState rather than the value that's provided from ViewData or the helper method. The assumption is that if you're re-rendering the form on postback, then it's because there was an error, and we should show the values the user typed rather than the values in the object.
In your controller action, you could remove the hidden value from ModelState to force it to use the new value.
Michelee
Member
1 Points
2 Posts
Re: Hidden doesn't get value from TempData
Oct 02, 2009 07:37 AM|LINK
Yes, I managed to resolve my problem. You ware right, this line do the trick:
ModelState.Remove("WrongTries");
Thanks.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Hidden doesn't get value from TempData
Oct 03, 2009 12:40 AM|LINK
Wouldn't it be simpler to keep track of this number in session state?
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hidden doesn't get value from TempData
Oct 15, 2009 06:07 AM|LINK
This is a question because I do not know the answer and I am unsure whether my understanding is correct.
It seems that the idea of ASP.NET MVC is to be "stateless" and that using "session state" would break that mould.
N'est-ce pas?
Gerry
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Hidden doesn't get value from TempData
Oct 15, 2009 06:24 PM|LINK
I use session state in many of my samples to simulate a DB (persistence). Session used judiciously is fine.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hidden doesn't get value from TempData
Oct 27, 2009 08:30 PM|LINK
interesting side effect of ModelState.Remove
if (String.IsNullOrEmpty(Brad)) // we have to have a Brad
ModelState.AddModelError("Brad", "Brad is needed.");
ModelState.Remove("Brad"); // we want a new Brad !!
^^^^^^^^^^^^^^^^^^^^^ PROBLEM: the error message also vanishes.
ONE SOLUTION: clone Brad
if (String.IsNullOrEmpty(Brad)) // we have to have a Brad
ModelState.AddModelError("Brad2", "Brad is needed."); // clone of Brad
ModelState.Remove("Brad"); // we want a new Brad !!
To every solution it sometimes seems there is another challenge to be solved.
Gerry
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Hidden doesn't get value from TempData
Oct 28, 2009 04:10 PM|LINK
I don't understand... your hidden fields are generating model state errors? How is that possible? Did you generate the hidden field with an invalid value?