Ajax将复杂对象发布到MVC控制器

时间:2014-03-10 18:48:06

标签: c# javascript ajax asp.net-mvc json

我想将一个复杂的对象发布到MVC控制器。

我发布到控制器的代码

         var url = "/admin/addevent"
         $.ajax({
            type: "POST",
            contentType: 'application/json',
            dataType: "json",
            url: url,
            data: MyNewEvent,
            success: success
        });

        function success() {
            alert("Success");
        }

以下对象被发布并被识别并解析到控制器的对象中:

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

以下对象

    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'
        }
    });

C#控制器

[HttpPost]
public JsonResult AddEvent(Event MyNewEvent)
{
    return Json(new { Success = true, Description = "Added" });
}

以下是它应解析的事件对象

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;
    }

}

似乎控制器无法识别MemberShip对象....有什么建议吗?

由于

2 个答案:

答案 0 :(得分:1)

Ok解决了这个问题,并且对于任何试图从我上面提供的细节解决它的人来说,你是公平的,你将无法做到。 (我现在更新了)。简而言之,MembershipType对象需要一个无参数构造函数,其中只有一个参数

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;
    }

}

当我用fiddler检查错误时,错误来自服务器端“没有为此对象定义无参数构造函数。”一旦我输入参数less constructor就解决了我的问题。

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()
    {
    }
}

答案 1 :(得分:0)

试试以下内容。删除JSON.stringify函数。

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