繁体   English   中英

Model state 无效,当列表超过一项时

[英]Model state is not valid, when list has more than one item

我正在使用以下代码从 ui 获取信息。 我使用 select2 as Combobox 让用户 select 多于一个元素。

var saveObj = {
        
        DiscountRate: $('#discountRate').val(),
        State: $('#isActiveSelect2List').val() == 1 ? true : false,
        AuthorList: $('#authorSelect2List').val(),
        InterpreterList: $('#interpreterSelect2List').val(),
        TagList: $('#tagSelect2List').val()
    };

我将此数据保存为 object,然后我将数据与 ajax 一起发布。但是如果我 select 超过一个用于 select2 的元素。 我的 controller 出错了;

错误

但是如果我 select 只有一个元素用于 select2 没关系,Model state 是真的。 我不知道我哪里错了。 有什么建议吗?

这是我的数据 model;

    public List<int> AuthorList { get; set; }
    public List<int> InterpreterList { get; set; }
    public List<int> TagList { get; set; }

使用 ajax 发布数据时,您正在传递 select2 列表的逗号分隔值。val() 返回逗号分隔字符串中的值,您需要在发布数据之前将此字符串转换为数组,如下所示:

var authorSelectedList = [];
var interpreterSelectedList= [];
var tagSelectedList= [];
if ($("#authorSelect2List").select2('data').length){
  $.each($("#authorSelect2List").select2('data'), function(key, item){
    authorSelectedList.push(item.text);
  });
}
if ($("#interpreterSelect2List").select2('data').length){
  $.each($("#interpreterSelect2List").select2('data'), function(key, item){
    interpreterSelectedList.push(item.text);
  });
}
if ($("#tagSelect2List").select2('data').length){
  $.each($("#tagSelect2List").select2('data'), function(key, item){
    tagSelectedList.push(item.text);
  });
}
var saveObj = {
    
    DiscountRate: $('#discountRate').val(),
    State: $('#isActiveSelect2List').val() == 1 ? true : false,
    AuthorList: authorSelectedList,
    InterpreterList: interpreterSelectedList,
    TagList: tagSelectedList
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM