AdaptiveChoiceSetInput默认选中后取消勾选提交异常问题
问题解决:Teams自适应卡片多选复选框提交异常
问题描述
在Microsoft Teams中创建机器人,使用自适应卡片的多选复选框控件,默认选中两个选项,但取消其中一个并点击提交按钮时,系统判定所有复选框均未选中,触发必填校验提示。
问题原因
代码中给AdaptiveChoiceSetInput的Value属性赋值为逗号分隔的字符串"1,2",但当IsMultiSelect = true时,多选复选框的Value应该是字符串数组类型,而非单个字符串。这种格式不匹配导致提交时控件无法正确识别剩余选中的选项,误判为未选中任何项。
修复后的代码
将chkBox.Value = "1,2";替换为字符串数组赋值:
var card = new AdaptiveCard("1.5"); AdaptiveChoice choice = new AdaptiveChoice(); var chkBox = new AdaptiveChoiceSetInput(); card.Body.Add(chkBox); chkBox.Spacing = AdaptiveSpacing.Medium; chkBox.IsMultiSelect = true; chkBox.Style = AdaptiveChoiceInputStyle.Expanded; chkBox.Id = "test"; chkBox.Wrap = true; // 修改此处:用字符串数组设置默认选中项 chkBox.Value = new List<string> { "1", "2" }; chkBox.IsRequired = true; chkBox.ErrorMessage = "please select at least one option."; choice = new AdaptiveChoice(); choice.Title = "Red"; choice.Value = "1"; chkBox.Choices.Add(choice); choice = new AdaptiveChoice(); choice.Title = "Orange"; choice.Value = "2"; chkBox.Choices.Add(choice); AdaptiveAction action = new AdaptiveSubmitAction(); action.Type = "Action.Submit"; action.Title = texts["btnConfirm"]; action.AdditionalProperties.Add("data", new Dictionary<string, object>() { { "id", "Testingsss" } }); card.Actions.Add(action); var act = MessageFactory.Attachment(new Attachment() { Content = card, ContentType = AdaptiveCard.ContentType }); await turnContext.SendActivityAsync(act);
说明
当启用多选(IsMultiSelect = true)时,AdaptiveChoiceSetInput的Value属性需要接收IList<string>类型的值,用来指定默认选中的选项集合。使用逗号分隔的字符串会导致控件解析错误,进而在提交时无法正确识别选中状态。
内容的提问来源于stack exchange,提问作者Swapnil Chowkekar
相关产品推荐
相关产品推荐

