开发者

MVC2 Noobie Question - How to Ajax post data to a controller action?

开发者 https://www.devze.com 2023-01-24 02:43 出处:网络
I have a really simple request, but I\'m stuck on how to implement the ajax stuff. Basically on document.ready, I\'ve got a javascript method that checks for a \"UTCOffset\" cookie, and if it doesn\'

I have a really simple request, but I'm stuck on how to implement the ajax stuff.

Basically on document.ready, I've got a javascript method that checks for a "UTCOffset" cookie, and if it doesn't exist, it will do a "behinds the scenes" ajax post to a controller that sets the offset.

Basically I'm trying to make it so that the user doesn't have to manually input their UTC offset.

How would I implement the Ajax post to the Action? Is the action 开发者_C百科supposed to be a "Sub" instead of a "Function" (since functions are supposed to return something.


You can use $.post (or regular $.ajax).

$.post("home/save", data, function (data) {
   // handle result, or ignore it if you don't care.
});

Your action method might look like this:

[HttpPost]
public ActionResult Save(int someId)
{
   // do the foo

   var result = new { code: 0 }; // or code : 1 if error occured (for example)
   return Json(result);
}
0

精彩评论

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