Checkbox input always return false value










0















I'm using asp.net core 2.1 and I have a simple view with form like:



@model Security.WebUi.Pages.AssignClaimToUserModel

<form method="post">
<div>
<label>User: </label>
<select asp-for="UserId" asp-items="@Model.UserList">
<option>Please select one</option>
</select>
</div>
<div>
<label>Role?</label>
<input type="checkbox" name="IsRole" id="isRole" />
</div>
<div>
<label>Claim Type</label>
<input type="text" name="ClaimType" id="claimType" />
</div>
<div>
<label>Claim Value</label>
<input type="text" name="ClaimValue" />
</div>
<button type="submit">Submit</button>
</form>


As you can see I have a checkbox with IsRole property



So in my model I have a boolean:



 public class ClaimToUserdModel

public string ClaimType get; set;
public string ClaimValue get; set;
public Guid UserId get; set;
public bool IsRole get; set;



then in method I call as:



public async Task<IActionResult> OnPost(ClaimToUserdModel model)

....



But it always throw false, it does not care about checked or not. What am I doing wrong?










share|improve this question

















  • 2





    By default a bool is false unless you set it to true somewhere in the code.

    – Casperonian
    Nov 15 '18 at 23:39















0















I'm using asp.net core 2.1 and I have a simple view with form like:



@model Security.WebUi.Pages.AssignClaimToUserModel

<form method="post">
<div>
<label>User: </label>
<select asp-for="UserId" asp-items="@Model.UserList">
<option>Please select one</option>
</select>
</div>
<div>
<label>Role?</label>
<input type="checkbox" name="IsRole" id="isRole" />
</div>
<div>
<label>Claim Type</label>
<input type="text" name="ClaimType" id="claimType" />
</div>
<div>
<label>Claim Value</label>
<input type="text" name="ClaimValue" />
</div>
<button type="submit">Submit</button>
</form>


As you can see I have a checkbox with IsRole property



So in my model I have a boolean:



 public class ClaimToUserdModel

public string ClaimType get; set;
public string ClaimValue get; set;
public Guid UserId get; set;
public bool IsRole get; set;



then in method I call as:



public async Task<IActionResult> OnPost(ClaimToUserdModel model)

....



But it always throw false, it does not care about checked or not. What am I doing wrong?










share|improve this question

















  • 2





    By default a bool is false unless you set it to true somewhere in the code.

    – Casperonian
    Nov 15 '18 at 23:39













0












0








0








I'm using asp.net core 2.1 and I have a simple view with form like:



@model Security.WebUi.Pages.AssignClaimToUserModel

<form method="post">
<div>
<label>User: </label>
<select asp-for="UserId" asp-items="@Model.UserList">
<option>Please select one</option>
</select>
</div>
<div>
<label>Role?</label>
<input type="checkbox" name="IsRole" id="isRole" />
</div>
<div>
<label>Claim Type</label>
<input type="text" name="ClaimType" id="claimType" />
</div>
<div>
<label>Claim Value</label>
<input type="text" name="ClaimValue" />
</div>
<button type="submit">Submit</button>
</form>


As you can see I have a checkbox with IsRole property



So in my model I have a boolean:



 public class ClaimToUserdModel

public string ClaimType get; set;
public string ClaimValue get; set;
public Guid UserId get; set;
public bool IsRole get; set;



then in method I call as:



public async Task<IActionResult> OnPost(ClaimToUserdModel model)

....



But it always throw false, it does not care about checked or not. What am I doing wrong?










share|improve this question














I'm using asp.net core 2.1 and I have a simple view with form like:



@model Security.WebUi.Pages.AssignClaimToUserModel

<form method="post">
<div>
<label>User: </label>
<select asp-for="UserId" asp-items="@Model.UserList">
<option>Please select one</option>
</select>
</div>
<div>
<label>Role?</label>
<input type="checkbox" name="IsRole" id="isRole" />
</div>
<div>
<label>Claim Type</label>
<input type="text" name="ClaimType" id="claimType" />
</div>
<div>
<label>Claim Value</label>
<input type="text" name="ClaimValue" />
</div>
<button type="submit">Submit</button>
</form>


As you can see I have a checkbox with IsRole property



So in my model I have a boolean:



 public class ClaimToUserdModel

public string ClaimType get; set;
public string ClaimValue get; set;
public Guid UserId get; set;
public bool IsRole get; set;



then in method I call as:



public async Task<IActionResult> OnPost(ClaimToUserdModel model)

....



But it always throw false, it does not care about checked or not. What am I doing wrong?







c# asp.net asp.net-core asp.net-core-2.1






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 22:55









JonathanJonathan

26011




26011







  • 2





    By default a bool is false unless you set it to true somewhere in the code.

    – Casperonian
    Nov 15 '18 at 23:39












  • 2





    By default a bool is false unless you set it to true somewhere in the code.

    – Casperonian
    Nov 15 '18 at 23:39







2




2





By default a bool is false unless you set it to true somewhere in the code.

– Casperonian
Nov 15 '18 at 23:39





By default a bool is false unless you set it to true somewhere in the code.

– Casperonian
Nov 15 '18 at 23:39












2 Answers
2






active

oldest

votes


















1














I got the same issue, I fixed it by writing html checkbox tag, giving it the name same as property name, and value = true, if the checkbox is not checked no need to worry as it won't be submitted anyway, in your case this will be it



<input type="checkbox" name="Remember" value="true" checked="@Model.YourmodelPropertyname"/>


also you set checkbox checked property value using jquery.






share|improve this answer






























    0














    You can use try :



    <input type="checkbox" name="IsRole" id="isRole" value="true" />


    It seems you are using ASP.NET Core Razor Page ,you can also refer to below sample :



    @page
    @model razorpages.Pages.AssignClaimToUserModel
    @
    ViewData["Title"] = "AssignClaimToUser";


    <form method="post">

    <div>
    <label>Role?</label>
    <input asp-for="ClaimToUserdModel.IsRole" name="IsRole">
    </div>

    <button type="submit">Submit</button>




    Code behind :



    public class AssignClaimToUserModel : PageModel

    public ClaimToUserdModel ClaimToUserdModel;
    public void OnGet()




    public async Task<IActionResult> OnPost(ClaimToUserdModel model)

    return null;



    public class ClaimToUserdModel

    public string ClaimType get; set;
    public string ClaimValue get; set;
    public Guid UserId get; set;
    public bool IsRole get; set;






    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%2f53329036%2fcheckbox-input-always-return-false-value%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I got the same issue, I fixed it by writing html checkbox tag, giving it the name same as property name, and value = true, if the checkbox is not checked no need to worry as it won't be submitted anyway, in your case this will be it



      <input type="checkbox" name="Remember" value="true" checked="@Model.YourmodelPropertyname"/>


      also you set checkbox checked property value using jquery.






      share|improve this answer



























        1














        I got the same issue, I fixed it by writing html checkbox tag, giving it the name same as property name, and value = true, if the checkbox is not checked no need to worry as it won't be submitted anyway, in your case this will be it



        <input type="checkbox" name="Remember" value="true" checked="@Model.YourmodelPropertyname"/>


        also you set checkbox checked property value using jquery.






        share|improve this answer

























          1












          1








          1







          I got the same issue, I fixed it by writing html checkbox tag, giving it the name same as property name, and value = true, if the checkbox is not checked no need to worry as it won't be submitted anyway, in your case this will be it



          <input type="checkbox" name="Remember" value="true" checked="@Model.YourmodelPropertyname"/>


          also you set checkbox checked property value using jquery.






          share|improve this answer













          I got the same issue, I fixed it by writing html checkbox tag, giving it the name same as property name, and value = true, if the checkbox is not checked no need to worry as it won't be submitted anyway, in your case this will be it



          <input type="checkbox" name="Remember" value="true" checked="@Model.YourmodelPropertyname"/>


          also you set checkbox checked property value using jquery.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 5:28









          A.M. PatelA.M. Patel

          689




          689























              0














              You can use try :



              <input type="checkbox" name="IsRole" id="isRole" value="true" />


              It seems you are using ASP.NET Core Razor Page ,you can also refer to below sample :



              @page
              @model razorpages.Pages.AssignClaimToUserModel
              @
              ViewData["Title"] = "AssignClaimToUser";


              <form method="post">

              <div>
              <label>Role?</label>
              <input asp-for="ClaimToUserdModel.IsRole" name="IsRole">
              </div>

              <button type="submit">Submit</button>




              Code behind :



              public class AssignClaimToUserModel : PageModel

              public ClaimToUserdModel ClaimToUserdModel;
              public void OnGet()




              public async Task<IActionResult> OnPost(ClaimToUserdModel model)

              return null;



              public class ClaimToUserdModel

              public string ClaimType get; set;
              public string ClaimValue get; set;
              public Guid UserId get; set;
              public bool IsRole get; set;






              share|improve this answer



























                0














                You can use try :



                <input type="checkbox" name="IsRole" id="isRole" value="true" />


                It seems you are using ASP.NET Core Razor Page ,you can also refer to below sample :



                @page
                @model razorpages.Pages.AssignClaimToUserModel
                @
                ViewData["Title"] = "AssignClaimToUser";


                <form method="post">

                <div>
                <label>Role?</label>
                <input asp-for="ClaimToUserdModel.IsRole" name="IsRole">
                </div>

                <button type="submit">Submit</button>




                Code behind :



                public class AssignClaimToUserModel : PageModel

                public ClaimToUserdModel ClaimToUserdModel;
                public void OnGet()




                public async Task<IActionResult> OnPost(ClaimToUserdModel model)

                return null;



                public class ClaimToUserdModel

                public string ClaimType get; set;
                public string ClaimValue get; set;
                public Guid UserId get; set;
                public bool IsRole get; set;






                share|improve this answer

























                  0












                  0








                  0







                  You can use try :



                  <input type="checkbox" name="IsRole" id="isRole" value="true" />


                  It seems you are using ASP.NET Core Razor Page ,you can also refer to below sample :



                  @page
                  @model razorpages.Pages.AssignClaimToUserModel
                  @
                  ViewData["Title"] = "AssignClaimToUser";


                  <form method="post">

                  <div>
                  <label>Role?</label>
                  <input asp-for="ClaimToUserdModel.IsRole" name="IsRole">
                  </div>

                  <button type="submit">Submit</button>




                  Code behind :



                  public class AssignClaimToUserModel : PageModel

                  public ClaimToUserdModel ClaimToUserdModel;
                  public void OnGet()




                  public async Task<IActionResult> OnPost(ClaimToUserdModel model)

                  return null;



                  public class ClaimToUserdModel

                  public string ClaimType get; set;
                  public string ClaimValue get; set;
                  public Guid UserId get; set;
                  public bool IsRole get; set;






                  share|improve this answer













                  You can use try :



                  <input type="checkbox" name="IsRole" id="isRole" value="true" />


                  It seems you are using ASP.NET Core Razor Page ,you can also refer to below sample :



                  @page
                  @model razorpages.Pages.AssignClaimToUserModel
                  @
                  ViewData["Title"] = "AssignClaimToUser";


                  <form method="post">

                  <div>
                  <label>Role?</label>
                  <input asp-for="ClaimToUserdModel.IsRole" name="IsRole">
                  </div>

                  <button type="submit">Submit</button>




                  Code behind :



                  public class AssignClaimToUserModel : PageModel

                  public ClaimToUserdModel ClaimToUserdModel;
                  public void OnGet()




                  public async Task<IActionResult> OnPost(ClaimToUserdModel model)

                  return null;



                  public class ClaimToUserdModel

                  public string ClaimType get; set;
                  public string ClaimValue get; set;
                  public Guid UserId get; set;
                  public bool IsRole get; set;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 3:40









                  Nan YuNan Yu

                  7,1652761




                  7,1652761



























                      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%2f53329036%2fcheckbox-input-always-return-false-value%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党