gpt4 book ai didi

asp.net-mvc-3 - JSON 和 ASP.Net MVC Controller 不绑定(bind)对象列表的属性

转载 作者:行者123 更新时间:2023-12-01 03:17:11 24 4
gpt4 key购买 nike

我正在使用 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;
}

和 Controller :

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

发生的事情是:

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

It have the correct number of objects, but is not binding the properties

为什么它没有绑定(bind)到属性?

最佳答案

绑定(bind)在 TopicsModel 对象中找不到 TopicsModel 属性,因此无法绑定(bind)该值。

试试这个:

{"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":""}]}

然后绑定(bind)应该正常恢复。

<小时/>

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

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

关于asp.net-mvc-3 - JSON 和 ASP.Net MVC Controller 不绑定(bind)对象列表的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14401171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com