how to get checkbox value in bool to model in . net core. IsEnable value as array in serialize

Mathu 0 Reputation points
2023-12-11T09:47:57.8933333+00:00

Capture

 public class AreaModel
 {
      public int AreaId { get; set; }
     public bool IsEnable { get; set; } = true;
 }
<form asp-area="MasterData" asp-controller="Home" asp-action="CreateUpdateArea" method="post">

    <div>
    @if (Model.AreaId != 0)
    {
        <div class="mb-3">
          <input asp-for="IsEnable" class="form-check-input" />
            <label asp-for="IsEnable" class="form-check-label">Enable</label>          
        </div>
    }
    <input asp-for="AreaId" type="hidden" />
    <div class="text-end">
        <button type="submit" class="btn btn-primary">Save</button>
    </div>
</form>
Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Mathu 0 Reputation points
    2023-12-11T09:50:36.4566667+00:00
    function serialize(data) {
        let obj = {};
    
        for (let [key, value] of data) {
            if (obj[key] !== undefined) {
                if (!Array.isArray(obj[key])) {
                    obj[key] = [obj[key]];
                }
    
                obj[key].push(value);
            } else {
                obj[key] = value;
            }
        }
      
    
        return obj;
    }
    
    function bindFormContent(dialog, updateUrl, updateTargetId) {
        $('form', dialog).submit(function (e) {
            e.preventDefault();
    
            if (!$(this).valid()) {
                return false;
            }
    
            let formData = new FormData(this);
            let data = serialize(formData);
    
            // Find the submit button within the form
            var submitButton = $(this).find('button[type="submit"]');
    
            var initialText = submitButton.text();
    
            //Disable the submit button and change its text
            submitButton.prop('disabled', true).text('Processing...');
    
            fetchDataWithPost(this.action, data)
                .then((result) => {
                    // process the data
            
                });
    
        });
    
        return false;
    }
    
    
    0 comments No comments

  2. Qing Guo - MSFT 896 Reputation points Microsoft External Staff
    2023-12-12T03:55:33+00:00

    Hi @Mathu ,

    I test your provided code, and use F12 to see the source tab, the data like:

    wwww

    Then in the CreateUpdateArea action:

     [HttpPost]
     public IActionResult CreateUpdateArea(AreaModel areaModel )
     {...//do your staff}
    
    
    

    result:

    ll


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Qing Guo


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.