序列化对象为ajax / json未定义

时间:2017-06-25 04:36:30

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

我正在尝试使用AJAX / JSON将List MyModel传递给控制器​​,并注意到这些对象都是以未定义的方式传递的:

[表格发布数据]

undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=

以下是我的示例代码:

[模型]

public class ICalEvent
{
    public string EventTitle { get; set; }
    public string EventAbstract { get; set; }
    public string EventStartDate { get; set; }
    public string EventEndDate { get; set; }
    public string VenueTitle { get; set; }
    public string VenueAddress { get; set; }

}

[控制器]

[HttpPost]
public JsonResult JsonCreateICalEvents(IList<ICalEvent> iCalEvents)
{
    Dictionary<String, String> status = new Dictionary<string, string>();

    if (iCalEvents != null)
    {
        status.Add("status", "success");
    }
    else
    {
        status.Add("status", "No objects passed!");
    }

    return Json(status);
}

[查看]

List<ICalEvent> iCalEvents = new List<ICalEvent>();
.
.
.
ICalEvent iCalEvent = new ICalEvent
{
    EventTitle = eventTitle,
    EventAbstract = eventAbstract,
    EventStartDate = screeningDate.ToShortDateString(),
    EventEndDate = screeningDate.ToShortDateString(),
    VenueTitle = venueTitle,
    VenueAddress = venueAddress
};
.
.
.
iCalEvents.Add(iCalEvent);


<script>
    $(document).ready(function () {
        $("#ical-events").click(function () {
            $.ajax({
                type: 'POST',
                url: '@Url.Action("JsonCreateICalEvents", "ICalEvent")',
                data: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(iCalEvents,Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonSerializerSettings { StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.Default })),
                dataType: 'json',
                success: function (data) {
                    if (data.status == "success") {
                        alert("Worked!");
                    }
                    else if(data.status != "success")
                    {
                        alert("Did not work!");
                    }
                }
            });
            event.preventDefault();
        });
    });
</script>

当我在帖子看起来很好之前检查AJAX JSON数据时:

[{"EventTitle":"Opening Night: Keep the Change","EventAbstract":"Under the guise of a New York romantic comedy, Keep the Change does something quite radical: In a refreshingly honest way it portrays two adults on the autistic spectrum. David desperately wants to be seen as “normal,” but Sarah accepts who she is. Together they navigate the vicissitudes of a burgeoning relationship. Writer/director Rachel Israel has an obvious affection for her characters, which infuses this poignant and funny film from the first frame to the last.","EventStartDate":"7/20/2017","EventEndDate":"7/20/2017","VenueTitle":"Castro Theatre","VenueAddress":"429 Castro Street"},{"EventTitle":"More Alive Than Dead","EventAbstract":"The legacy of Sigmund Freud is a slippery subject indeed. Whatever your views on the founder of psychoanalysis, there is no denying his incalculable influence on science, art, culture and even language. More Alive Than Dead explores opinions on Freud over the years with a sense of humor accompanied by hilarious animation. Experts assess his influence on psychoanalysis, neurology, literature, the LGBT community, the economy and feminism. In other words, just about everything.","EventStartDate":"7/21/2017","EventEndDate":"7/21/2017","VenueTitle":"Castro Theatre","VenueAddress":"429 Castro Street"},...]

鉴于所有内容都是未定义的,我的控制器会收到空数据。知道我错过了什么吗?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您缺少为iCalEvents分配一个应与操作参数名称对应的边界名称。因此,您应该将ajax请求更新为:

$.ajax({
    type: 'POST',
    url: 'http://example.com',
    data: { iCalEvents: [{"EventTitle":"Opening Night: Keep the Change","EventAbstract":"Under the guise of a New York romantic comedy, Keep the Change does something quite radical: In a refreshingly honest way it portrays two adults on the autistic spectrum. David desperately wants to be seen as “normal,” but Sarah accepts who she is. Together they navigate the vicissitudes of a burgeoning relationship. Writer/director Rachel Israel has an obvious affection for her characters, which infuses this poignant and funny film from the first frame to the last.","EventStartDate":"7/20/2017","EventEndDate":"7/20/2017","VenueTitle":"Castro Theatre","VenueAddress":"429 Castro Street"},{"EventTitle":"More Alive Than Dead","EventAbstract":"The legacy of Sigmund Freud is a slippery subject indeed. Whatever your views on the founder of psychoanalysis, there is no denying his incalculable influence on science, art, culture and even language. More Alive Than Dead explores opinions on Freud over the years with a sense of humor accompanied by hilarious animation. Experts assess his influence on psychoanalysis, neurology, literature, the LGBT community, the economy and feminism. In other words, just about everything.","EventStartDate":"7/21/2017","EventEndDate":"7/21/2017","VenueTitle":"Castro Theatre","VenueAddress":"429 Castro Street"}]},
    dataType: 'json',
    success: function (data) {
        if (data.status == "success") {
            alert("Worked!");
        }
        else if(data.status != "success")
        {
            alert("Did not work!");
        }
    }

});