我正在尝试使用带有Microsoft Bot框架的FormBuilder构建表单。我需要动态添加选项。
我能够添加它(状态)但是当我选择其中一个状态时,流程就会自动结束。
请在下面找到示例代码:
public enum StatesOptions
{
}
public enum BranchOptions
{
}
[Serializable]
public class AppointmentQuery
{
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your Name, it is a mandatory field")]
public string Name;
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your Email, it is a mandatory field")]
[EmailAddressAttribute(ErrorMessage = "Email Id is invalid")]
public string Email;
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter appointment date, it is a mandatory field")]
[EmailAddressAttribute(ErrorMessage = "Appointment date is invalid")]
public DateTime ApptDate;
[Prompt("Please enter {&}")]
public StatesOptions States;
public string SelectedState;
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your Email, it is a mandatory field")]
[EmailAddressAttribute(ErrorMessage = "Email Id is invalid")]
public string NewEmail;
[Prompt("Please enter {&}")]
public BranchOptions? Branches;
public string SelectedBranch;
public static IForm<AppointmentQuery> BuildAppointmentBookingForm()
{
OnCompletionAsyncDelegate<AppointmentQuery> bookAppointment = async (context, state) =>
{
};
return new FormBuilder<AppointmentQuery>()
.Field(nameof(Name))
.Field(nameof(Email))
.Field(nameof(ApptDate))
//.Field(nameof(States))
.Field(new FieldReflector<AppointmentQuery>(nameof(States))
.SetActive((state) => true)
.SetPrompt(new PromptAttribute("Please select one of the state from the following options: {||}")
{
ChoiceStyle = ChoiceStyleOptions.Buttons
})
.SetDefine((state, field) =>
{
var data = new FeedStateData().feedData();
var states = data.Select(r => r.StateName);
foreach (var item in states)
{
field
.AddDescription(item, item)
.AddTerms(item, item);
//return true;
}
return Task.FromResult(true);
})
.SetNext((value, state) =>
{
var selection = (States)value;
state.SelectedState = selection.StateName.ToString();
return new NextStep(new[] { nameof(Branches) });
}))
.OnCompletion(bookAppointment)
.Build();
}
}
另外,我需要向它添加另一个动态字段分支,但是代码没有到达它,即分支,没有异常被抛出但是Form不起作用。
请查找分支字段的示例代码
.Field(new FieldReflector<AppointmentQuery>(nameof(Branches))
.SetActive((state) => true)
.SetPrompt(new PromptAttribute("Please select branch from following options: {||}")
{
ChoiceStyle = ChoiceStyleOptions.Buttons
})
.SetDefine((state, field) =>
{
var data = new FeedStateData().feedData();
var states = data.Select(r => r.StateName);
var bracnhList = data.Where(s => s.StateName == state.SelectedState).Select(a => a.Branches).FirstOrDefault().Select(n => n.BranchName).ToList();
foreach (var item in bracnhList)
{
field
.AddDescription(item, item)
.AddTerms(item);
}
return Task.FromResult(true);
})
.SetNext((value, state) =>
{
var selection = (Branch)value;
state.SelectedBranch = selection.ToString();
return new NextStep(new[] { nameof(Branches) });
}))
有人可以帮我这个吗?
答案 0 :(得分:0)
我得到了答案,每个字段项都会在每个响应中得到评估。因此,即使没有填充SelectedState的值,也会评估跟随行。
var bracnhList = data.Where(s => s.StateName == state.SelectedState).Select(a => a.Branches).FirstOrDefault().Select(n => n.BranchName).ToList();
要修理它,我已经检查了它。
if(!string.IsNullOrEmpty(state.SelectedState))
请建议是否有更好的方法。