Hidden doesn't get value from TempDatahttp://forums.asp.net/t/1476843.aspx/1?Hidden+doesn+t+get+value+from+TempDataWed, 28 Oct 2009 16:37:59 -040014768433435789http://forums.asp.net/p/1476843/3435789.aspx/1?Hidden+doesn+t+get+value+from+TempDataHidden doesn't get value from TempData <p>&nbsp;Hi, </p> <p>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(&lt;%= Html.Hidden(&quot;wrongAnswer&quot;,TempData[&quot;WrongAnswer&quot;]==null?&quot;0&quot;:TempData[&quot;WrongAnswer&quot;].ToString()) %&gt;).</p> <p>In my controller action, if I get a wrong answer&nbsp;I use Request[&quot;wrongAnswer&quot;] to get value I increment it and put it back to TempData and then return View(). First time I redirect to this action I&nbsp; put in TempData[&quot;WrongAnswer&quot;]=0. </p> <p>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?</p> 2009-10-01T14:37:05-04:003435913http://forums.asp.net/p/1476843/3435913.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>Try putting break points in your code and on your .aspx page if you have not already done so.</p> <p>You've a better chance of seeing what is going on if you patiently step through your .aspx and your code.</p> <p><br> </p> <p><br> </p> <p>Regards,<br> Gerry (Lowry)<br> </p> 2009-10-01T15:44:15-04:003435918http://forums.asp.net/p/1476843/3435918.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>P.S.:&nbsp; you might want to post more of your actual code.<br> </p> 2009-10-01T15:46:21-04:003436008http://forums.asp.net/p/1476843/3436008.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>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.</p> <p>In your controller action, you could remove the hidden value from ModelState to force it to use the new value.</p> 2009-10-01T16:36:52-04:003436922http://forums.asp.net/p/1476843/3436922.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>&nbsp;Yes, I managed to resolve my problem. You ware right, this line do the trick:&nbsp;</p> <p><font size="2"></p> <p>ModelState.Remove(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;WrongTries&quot;</font></font><font size="2">);</font></p> <p><font size="2">Thanks.</font></p> <p></p> 2009-10-02T07:37:08-04:003438217http://forums.asp.net/p/1476843/3438217.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>Wouldn't it be simpler to keep track of this number in session state?&nbsp;</p> 2009-10-03T00:40:53-04:003459213http://forums.asp.net/p/1476843/3459213.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ricka6</h4> Wouldn't it be simpler to keep track of this number in session state? </blockquote> <p></p> <p>This is a question because I do not know the answer and I am unsure whether my understanding is correct.</p> <p>It seems that the idea of ASP.NET MVC is to be &quot;stateless&quot; and that using &quot;session state&quot; would break that mould.</p> <p>N'est-ce pas?</p> <p><br> </p> <p><br> </p> <p>Gerry<br> </p> <p><br> </p> 2009-10-15T06:07:53-04:003460584http://forums.asp.net/p/1476843/3460584.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>I use session state in many of my samples to simulate a DB (persistence). Session used judiciously is fine.<br> </p> 2009-10-15T18:24:28-04:003480359http://forums.asp.net/p/1476843/3480359.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>interesting side effect of&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>ModelState.Remove</b></p> <p><b><br> </b></p> <p>if (String.IsNullOrEmpty(Brad))&nbsp; // we have to have a Brad<br> &nbsp;&nbsp; <b>ModelState</b>.AddModelError(&quot;Brad&quot;, &quot;Brad is needed.&quot;);</p> <p><b>ModelState.Remove</b>(&quot;Brad&quot;);&nbsp;&nbsp;&nbsp;&nbsp; // we want a new Brad !!</p> <p>^^^^^^^^^^^^^^^^^^^^^&nbsp; <i><b>PROBLEM:&nbsp; the error message also vanishes.</b></i></p> <p><br> </p> <p><i><b>ONE&nbsp;&nbsp; SOLUTION:&nbsp; clone Brad<br> </b></i></p> <p>if (String.IsNullOrEmpty(Brad))&nbsp; // we have to have a Brad<br> &nbsp;&nbsp; ModelState.AddModelError(&quot;<b>Brad<span style="font-size:medium">2</span></b>&quot;, &quot;Brad is needed.&quot;); // clone of Brad<br> </p> <p>ModelState.Remove(&quot;Brad&quot;);&nbsp;&nbsp;&nbsp;&nbsp; // we want a new Brad !!</p> <p>&nbsp;</p> <p><br> </p> <p>To every solution it <i>sometimes </i>seems there is another challenge to be solved.</p> <p><br> </p> <p>Gerry<br> </p> 2009-10-27T20:30:05-04:003481938http://forums.asp.net/p/1476843/3481938.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>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?</p> 2009-10-28T16:10:32-04:003481989http://forums.asp.net/p/1476843/3481989.aspx/1?Re+Hidden+doesn+t+get+value+from+TempDataRe: Hidden doesn't get value from TempData <p>Hi Brad,</p> <p>no, the hidden field is not generating an error.</p> <p>I create some random data to create an arithmetical form of captcha.</p> <p>Because of the RESTful nature of ASP.NET MVC, I encrypt the equation and pass it as a hidden field so that it can not easily be dissected by a bot.</p> <p>A non hidden textbox field is available for the end user's answer.</p> <p>If the end user fails to answer correctly, I eliminate both the wrong answer and the hidden, encrypted equation.</p> <p>Everytime the end user is returned to that page, she/he is given a new random equation to solve.</p> <p><br> </p> <p>Regards ~~ Gerry<br> </p> 2009-10-28T16:37:59-04:00