RKh
RKh

Reputation: 14159

jQuery Popup Save button to execute POST method

I am using below code to open a jQuery popup box containing a Text Box.

User has to fill the remarks and click Save button on the popup.

The popup has two buttons: Save and Cancel.

I tried calling an Action Method when Save is clicked, but each time it is calling the GET method. It is not calling the POST method.

On click of Save, the remarks entered should be posted to an Action Method.

What changes are required in this code?

<script type="text/javascript">
        $(function () {
        $('#btnclick').click(function () {
            $("#popupdiv").dialog({
                title: "Enter Remarks: ",
                width: 400,
                height: 200,
                modal: true,
                buttons: {
                    Save: function () {
                        //POST code here
                    },
                    Close: function () {
                        $(this).dialog('close');
                    }
                }
            });
        });
    })
    </script>

Upvotes: 0

Views: 544

Answers (1)

Theunis
Theunis

Reputation: 338

This is an ajax post example. Assuming your remarks are contained within a $('#remarks') you can try this.

var remarks = {remarks: $('#remarks').val()};
var json = JSON.stringify(remarks);
 $.ajax({
        url: "localhost/Home/RemarksPost",
        type: "post",
        data:  json,
        dataType: 'json',
        success: function (response) {
           // you will get response 

        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }


    });

and your action result will look something like this

[HttpPost]
public ActionResult RemarksPost(string remarks)
{
 //Your code here
}

Upvotes: 2

Related Questions