How do I parse variables from ajax to a controller and return a bool
I want to parse 2 strings (Just testing with one atm( consiting of a date from a datepicker and hour and minute from 2 textfields. But I cant figure out how to parse data from my ajax call to the method I call, I know its called as I checked with a breakpoint, but its parameter is just null.
My ajax:
$('.datein').change(function () {
alert("datein changed");
//send servervalidering ajax
var result = false;
$.ajax({ url: "Resource/isDateValid/",
data: ($('#resource_datein').val() + "-" + $('#resource_hourin').val() + "-" + $('#resource_minutein').val()),
type: "POST",
sucess: ajaxsuccess(result)
}
);
});
My methodcall on success
function ajaxsuccess(result) {
alert("ajax lykkes!" +开发者_如何学Go result);
}
And the controller method which is part of public class ResourceController : DataTablesController
[HttpPost]
public bool isDateValid(string dateIn)
{
return true;
}
string dateIn just returns null if I set a date
try something like
//js
var data = $("myform").serialize(); //Might be serializeArray() not on dev machine sorry
$.post("Resource/isDateValid/", data, function(data){
alert(data.Success);
}, "json");
Controller (assuming Resource is a class because of the underscore in the ids...)
[HttpPost]
public JsonResult(Resource resource) //or (DateTime resource_datein, int resource_hourin)
{
bool success = true;
//Do something with posted data
return Json(new { Success = success});
}
EDIT (re comment)
take the () off the AjaxSuccess function call in the $.post
$('.datein').change(function () {
//send servervalidering ajax
var data = $(".datein").serialize();
$.post("Resource/isDateValid", data, ajaxsuccess, "text");
});
with
function ajaxsuccess(data)
{
alert(data.Success); // in your case alert(data); because it's "text"?
}
also because you are not serializing the entire form your data should be:
var data = {dateIn : $(".datein").val()};
精彩评论