How to make speak stuff no selection by true or false if speak stuff is null or no values ?

Ahmed Salah Abed Elaziz 390 Reputation points
2024-01-14T12:11:30.44+00:00

I work on asp.net mvc web app I face issue i can't make check box no selection if value of speakstuff is null so i will not make default value false i need speak stuff when display for first time of user to display as null so how to change my code to accept if speakstuff is null then no checked by true or false my code as below :

    $(document).ready(function () {

        $('.speak-stuff-checkbox').on('change', function () {
            $('.speak-stuff-checkbox').not(this).prop('checked', false);
            var selectedValue = $(this).val();
            $('#SpeakStuff').val(selectedValue);
        });
      
        });

     @Html.Label("Did You Meet/Speak to Staff", htmlAttributes: new { @class = "control-label col-md-5" })
     <div class="col-md-7">
        

         <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox" @(Model.SpeakStuff ? "checked" : "") disabled />
         Yes
         &nbsp;&nbsp;
         <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox" @(!Model.SpeakStuff ? "checked" : "") disabled />
         No



     </div>

 public class ResignationRequester
 {
     
public int RequestNo { get; set; }
public bool SpeakStuff { get; set; }
 }

when assign value to speakstuff it made as below :
obj.SpeakStuff = F6000059["REEV01"].ToString().ToUpper() == "Y" ? true : false;
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,590 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,276 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 5,580 Reputation points Microsoft Employee
    2024-01-14T12:22:41.83+00:00

    Hello @Ahmed Salah Abed Elaziz In your current code, SpeakStuff is a non-nullable Boolean, which means it can only be true or false. If you want it to be able to represent a third state (i.e., null), you should change the type of SpeakStuff to bool? (nullable boolean) in your ResignationRequester class. Here’s how you can modify your class:

    public class ResignationRequester
    {
        public int RequestNo { get; set; }
        public bool? SpeakStuff { get; set; } // Nullable boolean
    }
    

    And in your view, you can handle the nullable boolean like this:

    <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox" @(Model.SpeakStuff == true ? "checked" : "") disabled />
    Yes
    &nbsp;&nbsp;
    <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox" @(Model.SpeakStuff == false ? "checked" : "") disabled />
    No
    

    In this code, if SpeakStuff is null, neither of the checkboxes will be checked. Please note that you might need to adjust your JavaScript and assignment code accordingly to handle the nullable boolean. Also, remember that the IDs of your HTML elements should be unique. In your code, both checkboxes have the same ID SpeakStuff, which might cause issues. You might want to consider giving them different IDs. I hope this helps! If this solved your query, please tag this answer as accepted, as it helps the further community readers who may be looking for similar answers.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.