By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Issue value of checkbox not affect on model property speakstuff when call action?
JavaScript

Javascript – Issue value of checkbox not affect on model property speakstuff when call action?

Admin
Last updated: 2024/02/10 at 1:44 PM
Admin
Share
2 Min Read
Issue value of checkbox not affect on model property speakstuff when call action?

Problem:

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

Contents
Problem:Solution:

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; }

   }

Solution:

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.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Class components referencing and setting their own static variables - what is the functional component equivalent? Javascript – Class components referencing and setting their own static variables – what is the functional component equivalent?
Next Article Append values in a sizing chart Javascript – Append values in a sizing chart
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?