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!
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
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.
Re: cant pass data to controller
Hello,
In which case, I will move your thread over to the ASP.Net MVC Forum.
Thanks
Gary
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.