开发者

Pass datetime to controller action with jquery-ajax

how do i pass a datetime using jquery ajax to a controller action? using jquery ui datepicker ie.textbox and calendar control.

The action is still controller/action/11/12/2011 which doesn't work...any help pleasE?

this is what i have:

$.ajax({
            url: '<%: Url.Action("myPartial", "myController") %>/' + dateString,
            type: "GET",
            dataTypeString: "html",
            success: function (response, status, xhr) {
                var container = $("#myContainer");
                container.html(response);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var container = $("#myContainer");
                container.html('<div class="Error">An error occurred&l开发者_StackOverflow社区t;/div>');
            }
        });

where var dateString = $('#myDate').val();


If you have an action method on controller like this:

[HttpGet]
public ActionResult SomeMethod(int objectId, DateTime startDate, DateTime endDate) {
...
}

you can use next construction in javascript:

var start = new Date();
var end = new Date();
end.setDate(start.getDate() - 1);
var parameters = { objectId: objId, startDate: start.toUTCString(), endDate: end.toUTCString() };
// ....

$.ajax({
    url: '/YourController/SomeMethod',
    method: 'GET',
    data: parameters,
    cache: false,
    success: function (data, textStatus, jqXHR) {
      //...
    },
    error: function (jqXHR, textStatus, errorThrown) {
      //...
    }
});

it is piece of my code for example. it is not exact solution, but you can use this example with some modification.


You could replace "/" with hiphen sign, or try using uriEncode function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜