Search code examples
jqueryasp.net-mvcpartialviews

jquery button in pop up


I used index.js file to write the code for create and open the popup on button click. Partial view is invoked as a popup.

$("#createForm").dialog({
    autoOpen: false,
    modal: true,
    width: 550,
    height: 420,
    open: function(event, ui) {
        $(".ui-dialog-titlebar-close").hide();
    }
});

$(".buttonCreate").button().click(function() {
    $.ajax({
        // Call CreatePartialView action method
        url: "/PurchaseInvoice/AddItemPartial",
        type: 'Get',
        success: function(data) {
            $("#createForm").dialog("open");
            $("#createForm").empty().append(data);
            $("#editForm").hide();
        },
        error: function() {
            alert("something seems wrong");
        }
    });
});   

the button in popup is used to invoke the function from another .js file. this button is not working first time when popup opens but when the popup opens after closing the popup by esc button then it is working right .

$("#PopupAdd").click(function() {
    //  On submit button click close dialog box
    $("#createForm").dialog("close");
    // Set inserted vlaues
    var Item = $("#ddlItem").val().toString();
    var Quantity = $("#Quantity").val();
    var Price = $("#Price").val();
    var hdnID = $("#hdnInvoiceID").val();
    // Call Create action method
    $.post('/PurchaseInvoice/Create', {
            "ddlItem": Item,
            "Quantity": Quantity,
            "Price": Price,
            "hdnInvoiceID": hdnID
        },
        function() {
            alert("Data Saved successfully");
            window.location.reload(true);

        });
});

The partial view is used as a popup on the button click partial view is called from the controller and showed in popup.


Solution

  • Add some event on document.ready function of that page whose button is not working first time like-->

    This is function for accept numeric values only in textboxes on keypress event. You can add any function to initialize the events of page then button may be working properly first time.

    $(document).ready(function () {    
                $("#Quantity").keypress(function (e) {
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                $("#errmsg").html("Digits Only").show().fadeOut("slow");
                return false;
            }
                });
    });