[英]Model state is not valid, when list has more than one item
I'm using the following code to get the information from ui.我正在使用以下代码从 ui 获取信息。 I use select2 as Combobox to let user select more than one elements.
我使用 select2 as Combobox 让用户 select 多于一个元素。
var saveObj = {
DiscountRate: $('#discountRate').val(),
State: $('#isActiveSelect2List').val() == 1 ? true : false,
AuthorList: $('#authorSelect2List').val(),
InterpreterList: $('#interpreterSelect2List').val(),
TagList: $('#tagSelect2List').val()
};
I save this data as object, then i post the data with ajax. But if i select more than one elements for select2's.我将此数据保存为 object,然后我将数据与 ajax 一起发布。但是如果我 select 超过一个用于 select2 的元素。 I get an error in my controller;
我的 controller 出错了;
But if i select only one element for select2 its okay, Model state is true.但是如果我 select 只有一个元素用于 select2 没关系,Model state 是真的。 I don't know where i am wrong.
我不知道我哪里错了。 Any advice?
有什么建议吗?
Here's my data model;这是我的数据 model;
public List<int> AuthorList { get; set; }
public List<int> InterpreterList { get; set; }
public List<int> TagList { get; set; }
You are passing comma-separated values of select2 lists when posting data with ajax. val() is returning values in the comma-separated string and you need to convert this string into an array before posting data like this:使用 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.