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 に答える