c# - Ajax Posting complex object to MVC controller -
i want post complex object mvc controller.
my code post controller
var url = "/admin/addevent" $.ajax({ type: "post", contenttype: 'application/json', datatype: "json", url: url, data: mynewevent, success: success }); function success() { alert("success"); }
the following object gets posted , recognised , parsed object @ controller:
var mynewevent = json.stringify({ id: 0, name: 'asdasd', description: 'description', datetime: new date().toisostring(), owner: 1, duration: 30, reoccuringtype: 0 });
the following object not
var url = "/admin/addevent" var mynewevent = json.stringify({ id: 0, name: 'asdasd', description: 'description', datetime: new date().toisostring(), owner: 1, duration: 30, reoccuringtype: 0, membershiptype: { id: 1, name: 'test' } });
the c# controller
[httppost] public jsonresult addevent(event mynewevent) { return json(new { success = true, description = "added" }); }
the following event object should parse into
public class event { public int id { get; set; } public string name { get; set; } public string description { get; set; } public datetime datetime { get; set; } public user owner { get; set; } public user instructor { get; set; } public int maxattendance { get; set; } public int duration { get; set; } public int reoccuringtype { get; set; } public membershiptype membershiptype { get; set; } public organisation organisation { get; set; } } public class membershiptype { public int id { get; set; } public string name { get; set; } public membershiptype(bo.membershiptype membershiptype) { this.id = membershiptype.id; this.name = membershiptype.name; } }
it seems controller cannot recognise membership object.... suggestions why?
thanks
ok solved issue , fair trying solve detail had provided above, not able to. (i have updated now). in short membershiptype object needed parameterless constructor had 1 took parameter
public class membershiptype { public int id { get; set; } public string name { get; set; } public membershiptype(bo.membershiptype membershiptype) { this.id = membershiptype.id; this.name = membershiptype.name; } }
when inspected error fiddler error server side "no parameterless constructor defined object. " once put in parameter less constructor solved problem.
public class membershiptype { public int id { get; set; } public string name { get; set; } public membershiptype(bo.membershiptype membershiptype) { this.id = membershiptype.id; this.name = membershiptype.name; } public membershiptype() { } }
Comments
Post a Comment