0

I've a main view having two partial view with same modal one as main address and another as delivery address. I've a checkbox(this is also my delivery address) under main address where on checking that delivery address should be removed from database as well as shouldn't be visible on the view.

Currently the problem that I'm facing right now is in a scenario where, if user click on checkbox when user already had a delivery address before, the delivery address will be removed from db as well as delivery partialview will be hidden. I'm using a jquery to trigger an action to remove from db and hide the delivery partial view. Below is the code for the same:

$('input[type="checkbox"]').on('change', function (e) {
        if (e.target.checked) {
            // call this if checkbox is checked
            $(document).ready(function () {
                $('#modal-delivery').modal('show');
            });
        }
        else
        {
            // call this if checkbox is unchecked
            $.post('@Url.Action("DeliveryAddressUpdate", "Address")',
           { data: address, isDeliveryAddressDeleted: true
           });
            $(document).ready(function () {
            //clear the modal here before showing the view
                $('#delivery-address').show().find('.btn-edit').click();

                //$('button[type="button"]').click();
            });
        }
    });

I've an action in the controller which will be triggered by above jquery, somthing like:

[HttpPost]
public ActionResult DeliveryAddressUpdate(AddressModel data, bool isDeliveryAddressDeleted)
{
     UpdateDeliveryAddress(data, isDeliveryAddressDeleted);
}

Now when I uncheck the checkbox, again it will trigger the same jquery and run the else part of it. While showing the delivery address it will still retain the previous delivery address values for some reason. When I refresh the page then it shows blank delivery field. Any idea on this cause.

I tried ModelState.Clear() but it doesn't work for me.

Any help will be appreciated.

Jilu
  • 67
  • 1
  • 2
  • 12
  • 2
    You can use `.one()` instead of `.on()` so that it only ever gets triggered once. But deleting the record when the checkbox is clicked is a bad idea (what if the user made a mistake and immediately un-checks it) You should bind the checkbox to a view model property and when the whole form is submitted, delete the record if its `true` (just toggle visibility when the checkbox is clicked) –  Mar 08 '17 at 07:43
  • 1
    use jQuery and manually reset every field using $(#fieldId).val('');I always use that.. – Dhiren Patel Mar 08 '17 at 07:45
  • @StephenMuecke Thanks for the reply. As per the requirement it suppose to work that way, as soon as user uncheck it has to delete. – Jilu Mar 08 '17 at 07:57
  • @DhirenPatel how about adding this way as shown below: `$('#delivery-address').show().find('.btn-edit').click().find('.form-control').val('');` as I've MVC controls somthing like this: `@Html.TextBoxFor(m => Model.City, htmlAttributes: new { @class = "form-control", maxlength = "30" })` – Jilu Mar 08 '17 at 07:58
  • http://stackoverflow.com/questions/32610270/how-to-render-partial-view-in-mvc5-via-ajax-call-to-a-controller-and-return-html use this one – Murad Mar 08 '17 at 07:59

0 Answers0