javascript - How to send AJAX/Jquery object to Controller (discrepancy) MVC4 -
i have had confusion while , it's because of following reason:
i trying send jquery object
var myobject = { title: $('#title').val(), title2: $('#title2').val(), title3: $('#title3').val(), };
through ajax call
$.ajax({ data: { myobjectname = myobject }, datatype: "json", url: "myurl", cache: false, error: function (ts) {//handle error}, success: function (result) {//handle success} });
i'm receiving object in controller this:
public actionresult myaction(objecttype myobjectname)
however, when receiving object javascript, not recognize such , instantiates new objecttype.
i know can send string, serialize if put object inside form, etc...what want know why approach seems work me (i have gotten work other objecttype(s)) , in other instances doesn't. have how complex object is? sending incomplete object? (the latter 1 doesn't seem since have sent 'incomplete' objects , empty fields instantiated null)
any insight appreciated. thanks!
change code to;
$.ajax({ data: myobject, datatype: "application/json", type: "post", url: "myurl", cache: false, error: function (ts) {//handle error}, success: function (result) {//handle success} });
notice type post means need add [httppost]
actioresult.
as long objecttype
class fields
public class objecttype { public string title { get; set; } public string title2 { get; set; } public string title3 { get; set; } }
your current code attempting bind model resemble
public class someobject { objecttype myobjectname { get; set; } } public class objecttype{ public string title { get; set; } public string title2 { get; set; } public string title3 { get; set; } }
Comments
Post a Comment