2

我正在使用 jQuery 进行 AJAX 调用:

        var topic = new Array();

        $('.container-topico').each(function (i) {
            topic.push(
            {
                "TopicsModel":
            {
                begins: $(this).find('.HoursStart').val(),
                ends: $(this).find('.HoursEnd').val(),
                texts: $(this).find('.input-topic').val()
            }
            }
                );
        });

        var data = JSON.stringify({
            videoId: '<%=Url.RequestContext.RouteData.Values["id"]%>',
            topics: topic
        });

        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: '<%= Url.Action("SubmitTopics") %>',
            traditional: true,
            data:
            data
        ,

            beforeSend: function (XMLHttpRequest) {
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            success: function (data, textStatus, XMLHttpRequest) {
                $(data).each(function () {
                });
            }
        });

它以以下格式发送 JSON(例如):

{"videoId":"1","topics":


[{"TopicsModel": {"begins":"00:00:33","ends":"00:01:00","texts":"1. Primeiro tema"}},
    {"TopicsModel": {"begins":"00:01:00","ends":"00:01:33","texts":"2. Segundo tema"}},    
    {"TopicsModel": {"begins":"00:01:33","ends":"00:02:00","texts":"3. Terceiro tema"}},
    {"TopicsModel": {"begins":"00:02:00","ends":"00:00:21","texts":"dasdasdsa ada as das s"}},
    {"TopicsModel": {"begins":"0","ends":"0","texts":""}}]}

在服务器端它有模型:

 public class TopicsModel
    {
        public string begins;
        public string ends;
        public string texts;
    }

和控制器:

public ActionResult SubmitTopics(int videoId, List<TopicsModel> topics)

发生的事情是:

它具有正确数量的对象,但没有绑定每个元素的属性,如下所示:

它具有正确数量的对象,但没有绑定属性

为什么它不绑定到属性?

4

2 回答 2

4

绑定无法TopicsModel在您的对象中找到该属性TopicsModel,因此无法绑定该值。

试试这个:

{"videoId":"1","topics":


[{"begins":"00:00:33","ends":"00:01:00","texts":"1. Primeiro tema"},
    {"begins":"00:01:00","ends":"00:01:33","texts":"2. Segundo tema"},    
    {"begins":"00:01:33","ends":"00:02:00","texts":"3. Terceiro tema"},
    {"begins":"00:02:00","ends":"00:00:21","texts":"dasdasdsa ada as das s"},
    {"begins":"0","ends":"0","texts":""}]}

然后绑定应正常恢复。


您还必须以这种方式更改模型:

public class TopicsModel
    {
        public string begins { get; set; }
        public string ends { get; set; }
        public string texts { get; set; }
    }
于 2013-01-18T15:42:40.677 回答
0

List<TopicsModel> topics尝试在控制器参数列表中重命名为List<TopicsModel> model

于 2013-01-18T15:42:56.510 回答