简体   繁体   中英

Ajax Posting complex object to MVC controller

I want to post a complex object to an MVC controller.

My code to post to the 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 and is recognised and parsed into the object at the controller:

var MyNewEvent = JSON.stringify({
        ID: 0,
        Name: 'asdasd',
        Description: 'Description',
        DateTime: new Date().toISOString(),
        Owner: 1, Duration: 30,
        ReoccuringType: 0
    });

the following object does 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 is the Event object it 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 the controller cannot recognise the MemberShip object.... Any suggestions why?

Thanks

Ok Solved the issue and to be fair to anyone trying to solve it from the detail I had provided above, you would not be able to. (I have updated it now). In short the MembershipType object needed a parameterless constructor where it only had one that took a 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 I inspected the error with fiddler the error was from the server side with "No parameterless constructor defined for this object. " Once I put in a parameter less constructor it solved my 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()
    {
    }
}

Try following. Remove JSON.stringify function.

var MyNewEvent = {
        ID: 0,
        Name: 'asdasd',
        Description: 'Description',
        DateTime: new Date().toISOString(),
        Owner: 1, Duration: 30,
        ReoccuringType: 0,
        MembershipType: {
            ID: 1,
            Name: 'Test'
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM