0

I work on asp.net MVC app . I face issue checkbox value not detected

changing value from true to false or reverse on model property speakstuff.

so I need if yes checked then true if no checked then false then affect this value on model property speakstuff .

html script represent yes or No check box

<table style="border: 1px solid black;width:100%;margin-bottom:5px;">

            <tr>
                <td style="width: 50%; font-weight: bold; padding-top: 10px;">
                    @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" : "") />
                        Yes
                        &nbsp;&nbsp;
                        <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox" @(!Model.SpeakStuff ? "checked" : "") />
                        No
                    </div>
                </td>
</table>

I access value from form by java script

<script type="text/javascript">
        function submit() {
            var ResignationRequester = new Object();
            ResignationRequester.SpeakStuff = document.getElementById("SpeakStuff").value;
            }
            

</script>


 jQuery code for single  check



$(document).ready(function () {
    $('.speak-stuff-checkbox').on('change', function () {
        $('.speak-stuff-checkbox').not(this).prop('checked', false);
    });

});

using (Html.BeginForm("ApprovalIndex", "Resignation", new { id = Model.RequestNo }, FormMethod.Post, htmlAttributes: new { @style = "display:inline;" }))
        {
            

            <a onclick="submit();" class="btn btn-primary" style="min-width: 100px;margin-top:5px;
            margin-left: 1300px;"><i class="glyphicon glyphicon-ok"></i> Approve </a>
            
        }



[HttpPost]
      
        public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ)
        {
        }

So How to solve this issue ?

updated post

model that represent property SpeakStuff

public class ResignationRequester
   {
       [Display(Name = "Employee No : ")]
       [Required]
       public int EmpID { get; set; }
       [Display(Name = "Request No")]
       public int RequestNo { get; set; }
       public bool SpeakStuff { get; set; }

   }
4
  • Isn't it your element name should be REQ_SpeakStuff since you are passing a modal over. Otherwise use Bind[] to bind the element to modal. Commented Oct 16, 2023 at 7:57
  • can you write more details if possible please i using asp.net mvc not razor page Commented Oct 16, 2023 at 8:10
  • Razor or not it is the same concept. Please include your model or view model so that I could answer it accordingly Commented Oct 16, 2023 at 8:41
  • i updated my post with modal so please check it Commented Oct 16, 2023 at 8:54

1 Answer 1

1

create a hidden field for SpeakStuff

@Html.HiddenFor(x => Model.SpeakStuff)

Remove The SpeakSuff property in the checkbox

<div class="col-md-7">
                        <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox") />
                        Yes
                        &nbsp;&nbsp;
                        <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox") />
                        No
                    </div>

Amend your javascript to update the value to the hidden field upon check selection

 $(document).ready(function () {
            $('.speak-stuff-checkbox').on('change', function () {
                $('.speak-stuff-checkbox').not(this).prop('checked', false);
               var res =  $(this).val();
                $('#Model_SpeakStuff').val($(this).val());

                //alert($('#test').val());
                
            });
        });

The speakstuff value is derived from the hidden field.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.