0

I am developing a discussion panel in which I am going to post the comment and want to save it in database through controller and using the following code

$("#btnPostComment").click(function () {
                var strdata = $("problemID").val() + $("empID").val() + $("#_1").val() + "0" + "0";
                $.ajax({
                    type: "POST",
                    URL: <%= Url.Action("PostComment", "Discussion")  %>,
                    data: 'strdata',
                    dataType: "bool",
                    success: function (msg) {
                        alert("Success" + msg);
                    }


                })
        })

or I am also trying

 var url = "/Controllers/DiscussionController/PostComment";

            $.post(url, { comment: "abc" }, function (data) {
                alert("d");
            })

but not getting the results

3
  • What does your action look like? Commented Jan 25, 2012 at 21:38
  • In my controller DiscussionController.cs contains the function public bool PostComment(String pFormData) { return false; } and i want to execute this function by this line <%= Url.Action("PostComment", "Discussion") %> but it is not running Commented Jan 25, 2012 at 21:48
  • Have you marked the method PostComment with the [HttpPost] attribute? It will not respond to a "POST" otherwise. Commented Jan 25, 2012 at 21:50

3 Answers 3

2

You should enclose the url in quotes as it is a string and the variable(strdata) should not be in quotes.

Also the data that you send to server should be in key/value pair format. Looking at your data it doesn't make any sense. I have modified strdata as well take a look. Add the data parameters in that way.

$("#btnPostComment").click(function () {
    var strdata = {
       problemID: $("problemID").val(),
       empID: $("#_1").val()
    };
    $.ajax({
        type: "POST",
        url: "<%= Url.Action("PostComment", "Discussion")  %>",
        data: strdata,
        success: function (msg) {
            alert("Success" + msg);
        }
    });
})
Sign up to request clarification or add additional context in comments.

3 Comments

In my controller DiscussionController.cs contains the function public bool PostComment(String pFormData) { return false; } and i want to execute this function by this line <%= Url.Action("PostComment", "Discussion") %> but it is not running
Also note that the hash key "URL:" needs to be lowercase, "url:"
Do you see the call being made?
0

The variable strdata should be passed as a variable, not in quotes. And the URL should be in quotes. Also, I think your selectors are incorrect that are going into strdata. You should have # in front of them if they're ID's.

$("#btnPostComment").click(function () { 
    var strdata = $("#problemID").val() + $("#empID").val() + $("#_1").val() + "0" + "0"; 
    $.ajax({ 
        type: "POST", 
        URL: "<%= Url.Action("PostComment", "Discussion")  %>", 
        data: strdata, 
        dataType: "bool", 
        success: function (msg) { 
            alert("Success" + msg); 
        } 
    }); 
}); 

1 Comment

In my controller DiscussionController.cs contains the function public bool PostComment(String pFormData) { return false; } and i want to execute this function by this line <%= Url.Action("PostComment", "Discussion") %> but it is not running
0

You should pass strdata var not "strdata" string

$("#btnPostComment").click(function () {
    var strdata = $("problemID").val() + $("empID").val() + $("#_1").val() + "0" + "0";
    $.ajax({
        type: "POST",
        url: '<%= Url.Action("PostComment", "Discussion", strdata)  %>',
        success: function (msg) {
            alert("Success" + msg);
        }
    });
});

2 Comments

In my controller DiscussionController.cs contains the function public bool PostComment(String pFormData) { return false; } and i want to execute this function by this line <%= Url.Action("PostComment", "Discussion") %> but it is not running
@Snake. I updated the code. You really should ask yourself why are you send AJAX post to an action that return false..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.