Results 1 to 5 of 5

Thread: cant pass data to controller

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    15

    cant pass data to controller

    Hi! im working on getting user input and passing it to the save controller to save it to the database but it doesnt seem to work. I did some searching and tried to use jquery.ajax and jquery.post but it still wont work. Heres my code:
    HTML Code:
    $.post("/PostCategories/Save",
            getDate(),
            function (data) {
                alert("Saved successfully!");
            });
    and I tried this one as well:

    HTML Code:
     $.ajax({
                    type: "POST",
                    url: "@Url.Action("PostCategories", "Save");",
                    dataType: "json",
                    data: getData(),
                    processData: true,
                    success: function (data) {
                        alert("Saved successfully!");
                        alert(data);
                    },
                    error: function (xhr, ajaxOptions, error) {
                        alert(xhr.status);
                        alert("Error: " + xhr.responseText);
                        
                    }
                }); // end of ajax
    I put a breakpoint in my save controller but it doesnt even reach the controller. Need help!

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: cant pass data to controller

    Hello,

    You use the word controller which makes me think that you are using ASP.Net MVC. Is that correct?

    Gary

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    15

    Re: cant pass data to controller

    Yes. By the way its working already. I've made some mistake with the url. the only problem im having now is the dialog not closing after saving the data to the database.
    Heres my code:
    Code:
    $(function () {
    
    
            $('#Save').click(function () {
    
    
                $('#JqPostForm').submit(function (e) {
                    e.preventDefault();
    
                   
    
                    var catName = $("#txtAddNewCategory").val();
                    var catSlug = $("#txtSlug").val();
                    var catParentID = $("#txtParent").val();
    
                    var json = {
                        "Name": catName,
                        "Slug": catSlug,
                        "ParentID": catParentID
                    };
    
    
                               if (json == null) {
                                alert("Form invalid!");
                                 return;
                                    }
                    
    
                 $.ajax({
                    type: "POST",
                    url: "@Url.Action("Save", "PostCategories")",
                    dataType: "json",
                    data: json,
                    processData: true,
                    success: function (data) {
                        alert("Saved successfully!");
                      
                        function () {  $(this).dialog("close"); 
                        $(this).empty();
                    }
                    
    
                }); // end of ajax
                    
            }); // end of form.submit
    
            }); //end of Save
    
        }); //end of function
    if i remove the function to close the dialog everything is saved to the database but with the dialog close function under the 'alert("Saved successfully!");' it wont show that alert and data not saved to db but of course the dialog closes itself as expected.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: cant pass data to controller

    Hello,

    In which case, I will move your thread over to the ASP.Net MVC Forum.

    Thanks

    Gary

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: cant pass data to controller

    You're missing the closing brace for your close dialog method.

    I would recommend you install Firebug if you're using Firefox or Chrome or running Developer Tools (F12) if you're using Internet Explorer to debug javascript.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width