Passing JSON objects from javascript to ASP.NET MVC
Hi i have some complex types in javascript and want to pass them to my ASP.NET MVC 开发者_如何学Goapp, the problem is how? I already added a custom ModelBinder (but i dont see why do i need to do that, unless i need to set some erros). The problem is i have;
var obj =
{
"intvar":"222",
"stringvar" :"31asd",
"datevar":new Date(123),
....
}
How do i pass this object to ASP.NET? via query string and via Post method. EDIT: Soz for the bad explanation what i want is:
public class SomeObj
{
public int intvar;
public string stringvar;
public DateTime datevar;
}
public class HomeController : Controller
{
public someActionMethod(SomeObj o)
{
}
} Thkz in advance.
Make an AJAX call:
var json =
{
intvar : 222,
stringvar: '31asd',
datevar: new Date(123)
}
$.ajax({
type: "POST",
url: "<%= Url.Action("handleJson", "<YOUR CONTROLLER NAME>") %>",
dataType: "json",
data: json,
success: function(data) {
alert(data);
}
});
Then your controller method could accept parameters named the same as your json properties:
public ActionResult handleJson(int intvar, string stringvar, DateTime datevar)
{
return Json("I did cool stuff but you can't see it :)");
}
Please see if this answer helps. Look especially at all the attributes that the service class and service method has.
Also this article has some tips this question could use
精彩评论