给出以下表单流程设置...
return builder
.Field(new FieldReflector<CarValuationDialog>(nameof(Mileage))
.SetValidate(async (state, value) =>
{
var result = new ValidateResult { IsValid = true };
if (int.TryParse(value.ToString(), out int mileage))
{
state.Mileage = mileage;
result.IsValid = true;
}
else
{
result.Feedback = "That isn't valid number. Can you enter it again please?";
result.IsValid = false;
}
return result;
}))
.Field(new FieldReflector<CarValuationDialog>(nameof(HappyWithAnswersBuying))
.SetActive(carValuationDialog => carValuationDialog.ValuationOption == ValuationOptions.LookingToOwn)
.SetPrompt(CreateHappyWithAnswersBuyingPrompt())
.SetDefine(HappyWithAnswersDefinitionMethod))
.OnCompletion(GetValuationAndDisplaySummaryToUser)
.Build();
我想尝试对用户给出的里程答案进行自定义验证。执行SetValidate
中的代码,并指定一个有效的整数,将该数字分配给state.Mileage
。
但是,当显示询问用户是否对答案满意的提示时,里程数仍设置为0。
这是提示的代码。
private static PromptAttribute CreateHappyWithAnswersBuyingPrompt()
{
StringBuilder sb = new StringBuilder(100);
sb.Append("Are you happy with your answers? <br /><br />");
sb.Append("{&RegistrationNumber}: {RegistrationNumber} <br />");
sb.Append("{&Mileage}: {Mileage:N0} miles {||}");
return new PromptAttribute(sb.ToString())
{
ChoiceStyle = ChoiceStyleOptions.Buttons,
FieldCase = CaseNormalization.InitialUpper
};
}
在添加SetValidate
代码之前,里程在提示中显示的很好。但是,由于我现在手动设置state.Mileage
,因此由于某种原因它不会一直存在。