lundi 15 octobre 2018

After uncheck the checkbox value is not inserted in mvc application

I have one checkbox and one textbox, now when I check the checkbox and write text in textbox at that time checkbox value and textbox value is inserted, but when I am uncheck the checkbox and write the text in textbox at that time textbox value is not inserted in database.

Here is my code

I used enum value for checkbox and textbox

Here is the enum code

public enum ChassisJobTypeEnum
{
    LinerChange = 1,
    ChainSetChange = 2  
}

Here is the view design code

<div class="form-group row">
    <label class="col-sm-3 col-form-label">Change Linear</label>
    <div class="col-sm-3">
        <input type="checkbox" class="form-group" id="ChassisJob@((int)ChassisJobTypeEnum.LinerChange)_Checked" placeholder="" />
    </div>
    <div class="col-sm-3">
        <input type="text" class="form-group" id="ChassisJob.@((int)ChassisJobTypeEnum.LinerChange)_OtherJob" placeholder="" />
    </div>
</div>
<div class="form-group row">
    <label class="col-sm-3 col-form-label">Change Chain Set</label>
    <div class="col-sm-3">
        <input type="checkbox" class="form-group" id="ChassisJob@((int)ChassisJobTypeEnum.ChainSetChange)_Checked" placeholder="" />
    </div>
    <div class="col-sm-3">
        <input type="text" class="form-group" id="ChassisJob.@((int)ChassisJobTypeEnum.ChainSetChange)_OtherJob" placeholder="" />
    </div>
</div>

<div class="form-group row">
    <div class="col-sm-12">
        <button type="submit" class="btn btn-primary" id="submit-form">Submit</button>
    </div>
</div>

Now, when submit the form at that time ajax is calling to the controller

$('form').submit(function (event) {
 event.preventDefault();

 var chassisJobs = [];
 chassisJob = {
     ChassisJobsTypeId: @((int)ChassisJobTypeEnum.LinerChange),                                 
     Checked: $("#ChassisJob@((int)ChassisJobTypeEnum.LinerChange)_Checked").prop('checked'),
     OtherJob: document.getElementById("ChassisJob.@((int)ChassisJobTypeEnum.LinerChange)_OtherJob").value,                                 
 };
 chassisJobs.push(chassisJob);

 chassisJob = {
    ChassisJobsTypeId: @((int)ChassisJobTypeEnum.ChainSetChange),
    Checked: $("#ChassisJob@((int)ChassisJobTypeEnum.ChainSetChange)_Checked").prop('checked'),
    OtherJob: document.getElementById("ChassisJob.@((int)ChassisJobTypeEnum.ChainSetChange)_OtherJob").value,
 };
 chassisJobs.push(chassisJob);

var serviceJobData = {
    'Id': document.getElementById("Id").value,                            
    'ChassisJob': chassisJobs
    };

     $.ajax({
         type: "POST",
         url: '/ServiceJob/AddUpdate',
         data: serviceJobData,
         dataType: "json",
         success: function (data) {
             if (data.success == true) {
                   alert("Data has been update successfully");

                 //RefreshTable();
                 location.reload(true);

      // set focus on edit form
      document.getElementById("service-job-list").scrollIntoView();

      //reste form
      var frm = document.getElementsByName('service-job-form')[0];
      frm.reset();

  }
  else {
        alert("Unable to insert data")
     }
             error: function (xhr, ajaxOptions, thrownError) {
             alert('Failed to add Detail.');
        }
   });
});

Here is the controller in which the addupdate method is calling at submit button.

[HttpPost]
public IActionResult AddUpdate(ServiceJobModel model)
{
    ServiceJob serviceJob = new ServiceJob();
    bool iSInsertData = true;

    var chassisJobs = _Db.ChassisJob.Where(x => x.ServiceJobId == model.Id).ToList();
    if (chassisJobs.Any())
    {
        _Db.ChassisJob.RemoveRange(chassisJobs);
        _Db.SaveChanges();
    }

    return Json(new { success = true });            
}

Now, this is code which I tried.

For more clear, let see task image.

When I check the checkbox and enter the value in textbox at that time insert data properly and at the fetch time it shows the checkbox and textbox value properly.

enter image description here

now, when I uncheck the checkbox and enter the value in textbox at that time data is not inserted and at the fetch time it does not show any thing.

enter image description here

If you any issue to understand the question then feel free to ask.




Aucun commentaire:

Enregistrer un commentaire