locked
Input checkbox not passing its Model Value to the controller if I make disabled using or condition RRS feed

  • Question

  • User-1355965324 posted

    I have given the code to make make the checkbox disabled conditionally , but when I call post method , always the value being passed as null rather than its model value. How can I pass model value to post method after disabled

    input type="checkbox" disabled="@(isdisabled || isDofDisabled ? "disabled" : null)" asp-for="@Model.attendanceLogList[i].IsDayOffMarked"  />

    Thursday, May 27, 2021 2:55 PM

Answers

  • User475983607 posted

    There are several issues.  Disabled inputs are not submitted by the browser use read only.   The checkbox has not been assigned a value, your design submits true/false.  A null value indicates a model binding issue with your design.    I recently provided a working example in one of your threads.  Please follow forum members recommendations.  If there is a problem with the recommendation then explain the unwanted behavior.

    I recommend learning HTML Forms.  This will help to create a proper input View rather than guessing.

    https://www.w3schools.com/html/html_forms.asp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 27, 2021 3:28 PM

All replies

  • User475983607 posted

    There are several issues.  Disabled inputs are not submitted by the browser use read only.   The checkbox has not been assigned a value, your design submits true/false.  A null value indicates a model binding issue with your design.    I recently provided a working example in one of your threads.  Please follow forum members recommendations.  If there is a problem with the recommendation then explain the unwanted behavior.

    I recommend learning HTML Forms.  This will help to create a proper input View rather than guessing.

    https://www.w3schools.com/html/html_forms.asp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 27, 2021 3:28 PM
  • User-474980206 posted

    the browser does not post back disabled fields. when the server disables the field, add a hidden field with the same name and and desired post back value.

    Thursday, May 27, 2021 3:28 PM
  • User-1355965324 posted

    mgebhard

    My issue was the  disabled checkbox is not passing the model value   while  submit. Then I have to add a hidden input type to pass the value to the controller

    Many Thanks to all

    Thursday, May 27, 2021 8:54 PM