Why mvc Html.HiddenFor does not render my field?












16















I'm trying to do this simple thing



<%= Html.HiddenFor(model => model.Id)%>


the model is



[HiddenInput(DisplayValue=true)]
public int Id { get; set; }


but i always get this rendered



<input type="hidden" value="0" name="UserInfo.Id" id="UserInfo_Id">


i've check and the id is NOT 0.. ?!



need some explanation here...



Edit



The problem seem's to be the post thing mentionned below.
This is working



<input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />


Thanks to Manaf










share|improve this question

























  • what happens when you put the same field in a normal textboxfor?

    – Stefanvds
    Aug 31 '10 at 12:35
















16















I'm trying to do this simple thing



<%= Html.HiddenFor(model => model.Id)%>


the model is



[HiddenInput(DisplayValue=true)]
public int Id { get; set; }


but i always get this rendered



<input type="hidden" value="0" name="UserInfo.Id" id="UserInfo_Id">


i've check and the id is NOT 0.. ?!



need some explanation here...



Edit



The problem seem's to be the post thing mentionned below.
This is working



<input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />


Thanks to Manaf










share|improve this question

























  • what happens when you put the same field in a normal textboxfor?

    – Stefanvds
    Aug 31 '10 at 12:35














16












16








16


5






I'm trying to do this simple thing



<%= Html.HiddenFor(model => model.Id)%>


the model is



[HiddenInput(DisplayValue=true)]
public int Id { get; set; }


but i always get this rendered



<input type="hidden" value="0" name="UserInfo.Id" id="UserInfo_Id">


i've check and the id is NOT 0.. ?!



need some explanation here...



Edit



The problem seem's to be the post thing mentionned below.
This is working



<input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />


Thanks to Manaf










share|improve this question
















I'm trying to do this simple thing



<%= Html.HiddenFor(model => model.Id)%>


the model is



[HiddenInput(DisplayValue=true)]
public int Id { get; set; }


but i always get this rendered



<input type="hidden" value="0" name="UserInfo.Id" id="UserInfo_Id">


i've check and the id is NOT 0.. ?!



need some explanation here...



Edit



The problem seem's to be the post thing mentionned below.
This is working



<input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />


Thanks to Manaf







asp.net-mvc-2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 '10 at 22:43







mateo

















asked Aug 31 '10 at 4:44









mateomateo

2641411




2641411













  • what happens when you put the same field in a normal textboxfor?

    – Stefanvds
    Aug 31 '10 at 12:35



















  • what happens when you put the same field in a normal textboxfor?

    – Stefanvds
    Aug 31 '10 at 12:35

















what happens when you put the same field in a normal textboxfor?

– Stefanvds
Aug 31 '10 at 12:35





what happens when you put the same field in a normal textboxfor?

– Stefanvds
Aug 31 '10 at 12:35












4 Answers
4






active

oldest

votes


















23














I'm not sure if this is the case with you but the Html.HiddenFor() "do not output correct values after a post if the value is changed during the post." and this is a not a bug it was designed that way.



Quick Fix :



Don't use the helper, try this instead :



<input type="hidden" value="<%= Html.AttributeEncode(model.Id) %>" id="Id" name="Id" />


Always worked for me :)






share|improve this answer


























  • Thanks that was the case.

    – mateo
    Sep 5 '10 at 20:30






  • 1





    in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

    – mateo
    Sep 5 '10 at 22:41











  • Glad i could help :)

    – Manaf Abu.Rous
    Sep 6 '10 at 1:06






  • 3





    Apparently it's not a bug. Unfortunately it's something I keep getting caught on :( The other option is to have a ModelState.Clear(); call in your controller action.

    – Darren Oster
    Nov 28 '12 at 4:11











  • Anybody have any idea on why it was designed this way, if it's not a bug?

    – gb2d
    Jun 26 '13 at 9:59



















5














To add to Manaf's correct answer--you note correctly that the problem occurs in controller actions that handle posts. I was getting the same problem in a controller action that handles a get when I explicitly pass a model to a view:



[HttpGet]
ActionResult SearchForSomething(SearchForm searchForm)
{
searchForm.MyId = SomeValueFromSession;
return View("SearchForSomething", searchForm);
}


In the view, this line that rendered a hidden input for MyId always rendered "0":



@Html.HiddenFor(m => m.MyId);


Per Darren Oster's suggestion I changed to the following and fixed the problem:



[HttpGet]
ActionResult SearchForSomething(SearchForm searchForm)
{
searchForm.MyId = SomeValueFromSession;
ModelState.Clear();
return View("SearchForSomething", searchForm);
}





share|improve this answer
























  • Both the normal <input> tag and ModelState.Clear() work for me, but I find it clearer to use the latter (with the proper comment ofr my followers) than mixing @Html.xxx and input tags in my views...

    – Fred Mauroy
    Aug 14 '18 at 8:58



















0














My comment is relegated to the last place (even I couldn't find it), so:



In case you don't want to clear the modelstate, as Darren Oster suggested, removing the problematic key worked for me: ModelState.Remove("HiddenKey")






share|improve this answer































    0














    I ran into this problem as well with @Html.HiddenFor.



    @Html.Hidden("Id", Model.Id) also gave value 0, but a foreign key field, e.g., @Html.Hidden("Model_Category_ModelId", Model.Category.ModelId) did work, while it @Html.HiddenFor(m => m.Category.ModelId) did not.



    My solution was to redirect to the get action, as described in ASP NET MVC Post Redirect Get Pattern.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3606087%2fwhy-mvc-html-hiddenfor-does-not-render-my-field%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      23














      I'm not sure if this is the case with you but the Html.HiddenFor() "do not output correct values after a post if the value is changed during the post." and this is a not a bug it was designed that way.



      Quick Fix :



      Don't use the helper, try this instead :



      <input type="hidden" value="<%= Html.AttributeEncode(model.Id) %>" id="Id" name="Id" />


      Always worked for me :)






      share|improve this answer


























      • Thanks that was the case.

        – mateo
        Sep 5 '10 at 20:30






      • 1





        in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

        – mateo
        Sep 5 '10 at 22:41











      • Glad i could help :)

        – Manaf Abu.Rous
        Sep 6 '10 at 1:06






      • 3





        Apparently it's not a bug. Unfortunately it's something I keep getting caught on :( The other option is to have a ModelState.Clear(); call in your controller action.

        – Darren Oster
        Nov 28 '12 at 4:11











      • Anybody have any idea on why it was designed this way, if it's not a bug?

        – gb2d
        Jun 26 '13 at 9:59
















      23














      I'm not sure if this is the case with you but the Html.HiddenFor() "do not output correct values after a post if the value is changed during the post." and this is a not a bug it was designed that way.



      Quick Fix :



      Don't use the helper, try this instead :



      <input type="hidden" value="<%= Html.AttributeEncode(model.Id) %>" id="Id" name="Id" />


      Always worked for me :)






      share|improve this answer


























      • Thanks that was the case.

        – mateo
        Sep 5 '10 at 20:30






      • 1





        in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

        – mateo
        Sep 5 '10 at 22:41











      • Glad i could help :)

        – Manaf Abu.Rous
        Sep 6 '10 at 1:06






      • 3





        Apparently it's not a bug. Unfortunately it's something I keep getting caught on :( The other option is to have a ModelState.Clear(); call in your controller action.

        – Darren Oster
        Nov 28 '12 at 4:11











      • Anybody have any idea on why it was designed this way, if it's not a bug?

        – gb2d
        Jun 26 '13 at 9:59














      23












      23








      23







      I'm not sure if this is the case with you but the Html.HiddenFor() "do not output correct values after a post if the value is changed during the post." and this is a not a bug it was designed that way.



      Quick Fix :



      Don't use the helper, try this instead :



      <input type="hidden" value="<%= Html.AttributeEncode(model.Id) %>" id="Id" name="Id" />


      Always worked for me :)






      share|improve this answer















      I'm not sure if this is the case with you but the Html.HiddenFor() "do not output correct values after a post if the value is changed during the post." and this is a not a bug it was designed that way.



      Quick Fix :



      Don't use the helper, try this instead :



      <input type="hidden" value="<%= Html.AttributeEncode(model.Id) %>" id="Id" name="Id" />


      Always worked for me :)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 20:35









      Joel

      17.1k25481




      17.1k25481










      answered Aug 31 '10 at 8:10









      Manaf Abu.RousManaf Abu.Rous

      2,1371723




      2,1371723













      • Thanks that was the case.

        – mateo
        Sep 5 '10 at 20:30






      • 1





        in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

        – mateo
        Sep 5 '10 at 22:41











      • Glad i could help :)

        – Manaf Abu.Rous
        Sep 6 '10 at 1:06






      • 3





        Apparently it's not a bug. Unfortunately it's something I keep getting caught on :( The other option is to have a ModelState.Clear(); call in your controller action.

        – Darren Oster
        Nov 28 '12 at 4:11











      • Anybody have any idea on why it was designed this way, if it's not a bug?

        – gb2d
        Jun 26 '13 at 9:59



















      • Thanks that was the case.

        – mateo
        Sep 5 '10 at 20:30






      • 1





        in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

        – mateo
        Sep 5 '10 at 22:41











      • Glad i could help :)

        – Manaf Abu.Rous
        Sep 6 '10 at 1:06






      • 3





        Apparently it's not a bug. Unfortunately it's something I keep getting caught on :( The other option is to have a ModelState.Clear(); call in your controller action.

        – Darren Oster
        Nov 28 '12 at 4:11











      • Anybody have any idea on why it was designed this way, if it's not a bug?

        – gb2d
        Jun 26 '13 at 9:59

















      Thanks that was the case.

      – mateo
      Sep 5 '10 at 20:30





      Thanks that was the case.

      – mateo
      Sep 5 '10 at 20:30




      1




      1





      in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

      – mateo
      Sep 5 '10 at 22:41





      in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

      – mateo
      Sep 5 '10 at 22:41













      Glad i could help :)

      – Manaf Abu.Rous
      Sep 6 '10 at 1:06





      Glad i could help :)

      – Manaf Abu.Rous
      Sep 6 '10 at 1:06




      3




      3





      Apparently it's not a bug. Unfortunately it's something I keep getting caught on :( The other option is to have a ModelState.Clear(); call in your controller action.

      – Darren Oster
      Nov 28 '12 at 4:11





      Apparently it's not a bug. Unfortunately it's something I keep getting caught on :( The other option is to have a ModelState.Clear(); call in your controller action.

      – Darren Oster
      Nov 28 '12 at 4:11













      Anybody have any idea on why it was designed this way, if it's not a bug?

      – gb2d
      Jun 26 '13 at 9:59





      Anybody have any idea on why it was designed this way, if it's not a bug?

      – gb2d
      Jun 26 '13 at 9:59













      5














      To add to Manaf's correct answer--you note correctly that the problem occurs in controller actions that handle posts. I was getting the same problem in a controller action that handles a get when I explicitly pass a model to a view:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      return View("SearchForSomething", searchForm);
      }


      In the view, this line that rendered a hidden input for MyId always rendered "0":



      @Html.HiddenFor(m => m.MyId);


      Per Darren Oster's suggestion I changed to the following and fixed the problem:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      ModelState.Clear();
      return View("SearchForSomething", searchForm);
      }





      share|improve this answer
























      • Both the normal <input> tag and ModelState.Clear() work for me, but I find it clearer to use the latter (with the proper comment ofr my followers) than mixing @Html.xxx and input tags in my views...

        – Fred Mauroy
        Aug 14 '18 at 8:58
















      5














      To add to Manaf's correct answer--you note correctly that the problem occurs in controller actions that handle posts. I was getting the same problem in a controller action that handles a get when I explicitly pass a model to a view:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      return View("SearchForSomething", searchForm);
      }


      In the view, this line that rendered a hidden input for MyId always rendered "0":



      @Html.HiddenFor(m => m.MyId);


      Per Darren Oster's suggestion I changed to the following and fixed the problem:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      ModelState.Clear();
      return View("SearchForSomething", searchForm);
      }





      share|improve this answer
























      • Both the normal <input> tag and ModelState.Clear() work for me, but I find it clearer to use the latter (with the proper comment ofr my followers) than mixing @Html.xxx and input tags in my views...

        – Fred Mauroy
        Aug 14 '18 at 8:58














      5












      5








      5







      To add to Manaf's correct answer--you note correctly that the problem occurs in controller actions that handle posts. I was getting the same problem in a controller action that handles a get when I explicitly pass a model to a view:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      return View("SearchForSomething", searchForm);
      }


      In the view, this line that rendered a hidden input for MyId always rendered "0":



      @Html.HiddenFor(m => m.MyId);


      Per Darren Oster's suggestion I changed to the following and fixed the problem:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      ModelState.Clear();
      return View("SearchForSomething", searchForm);
      }





      share|improve this answer













      To add to Manaf's correct answer--you note correctly that the problem occurs in controller actions that handle posts. I was getting the same problem in a controller action that handles a get when I explicitly pass a model to a view:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      return View("SearchForSomething", searchForm);
      }


      In the view, this line that rendered a hidden input for MyId always rendered "0":



      @Html.HiddenFor(m => m.MyId);


      Per Darren Oster's suggestion I changed to the following and fixed the problem:



      [HttpGet]
      ActionResult SearchForSomething(SearchForm searchForm)
      {
      searchForm.MyId = SomeValueFromSession;
      ModelState.Clear();
      return View("SearchForSomething", searchForm);
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Feb 7 '14 at 15:05









      Tom ReganTom Regan

      1,40412339




      1,40412339













      • Both the normal <input> tag and ModelState.Clear() work for me, but I find it clearer to use the latter (with the proper comment ofr my followers) than mixing @Html.xxx and input tags in my views...

        – Fred Mauroy
        Aug 14 '18 at 8:58



















      • Both the normal <input> tag and ModelState.Clear() work for me, but I find it clearer to use the latter (with the proper comment ofr my followers) than mixing @Html.xxx and input tags in my views...

        – Fred Mauroy
        Aug 14 '18 at 8:58

















      Both the normal <input> tag and ModelState.Clear() work for me, but I find it clearer to use the latter (with the proper comment ofr my followers) than mixing @Html.xxx and input tags in my views...

      – Fred Mauroy
      Aug 14 '18 at 8:58





      Both the normal <input> tag and ModelState.Clear() work for me, but I find it clearer to use the latter (with the proper comment ofr my followers) than mixing @Html.xxx and input tags in my views...

      – Fred Mauroy
      Aug 14 '18 at 8:58











      0














      My comment is relegated to the last place (even I couldn't find it), so:



      In case you don't want to clear the modelstate, as Darren Oster suggested, removing the problematic key worked for me: ModelState.Remove("HiddenKey")






      share|improve this answer




























        0














        My comment is relegated to the last place (even I couldn't find it), so:



        In case you don't want to clear the modelstate, as Darren Oster suggested, removing the problematic key worked for me: ModelState.Remove("HiddenKey")






        share|improve this answer


























          0












          0








          0







          My comment is relegated to the last place (even I couldn't find it), so:



          In case you don't want to clear the modelstate, as Darren Oster suggested, removing the problematic key worked for me: ModelState.Remove("HiddenKey")






          share|improve this answer













          My comment is relegated to the last place (even I couldn't find it), so:



          In case you don't want to clear the modelstate, as Darren Oster suggested, removing the problematic key worked for me: ModelState.Remove("HiddenKey")







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 16 '15 at 19:50









          DavidCDavidC

          4161017




          4161017























              0














              I ran into this problem as well with @Html.HiddenFor.



              @Html.Hidden("Id", Model.Id) also gave value 0, but a foreign key field, e.g., @Html.Hidden("Model_Category_ModelId", Model.Category.ModelId) did work, while it @Html.HiddenFor(m => m.Category.ModelId) did not.



              My solution was to redirect to the get action, as described in ASP NET MVC Post Redirect Get Pattern.






              share|improve this answer




























                0














                I ran into this problem as well with @Html.HiddenFor.



                @Html.Hidden("Id", Model.Id) also gave value 0, but a foreign key field, e.g., @Html.Hidden("Model_Category_ModelId", Model.Category.ModelId) did work, while it @Html.HiddenFor(m => m.Category.ModelId) did not.



                My solution was to redirect to the get action, as described in ASP NET MVC Post Redirect Get Pattern.






                share|improve this answer


























                  0












                  0








                  0







                  I ran into this problem as well with @Html.HiddenFor.



                  @Html.Hidden("Id", Model.Id) also gave value 0, but a foreign key field, e.g., @Html.Hidden("Model_Category_ModelId", Model.Category.ModelId) did work, while it @Html.HiddenFor(m => m.Category.ModelId) did not.



                  My solution was to redirect to the get action, as described in ASP NET MVC Post Redirect Get Pattern.






                  share|improve this answer













                  I ran into this problem as well with @Html.HiddenFor.



                  @Html.Hidden("Id", Model.Id) also gave value 0, but a foreign key field, e.g., @Html.Hidden("Model_Category_ModelId", Model.Category.ModelId) did work, while it @Html.HiddenFor(m => m.Category.ModelId) did not.



                  My solution was to redirect to the get action, as described in ASP NET MVC Post Redirect Get Pattern.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 13 '15 at 9:55









                  R. SchreursR. Schreurs

                  3,74232943




                  3,74232943






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3606087%2fwhy-mvc-html-hiddenfor-does-not-render-my-field%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python