我想用手动添加和动态添加的字段填充表单。
public class ModelClass
{
[Prompt("URL?")]
public string URL { get; set; }
[Prompt("Name?")]
public string Title { get; set; }
}
表单生成器:
public IForm<ModelClass> BuildApplyform()
{
var builder = new FormBuilder<ModelClass>();
// Parse string to json for usage in the foreach
dynamic json = JObject.Parse(jsonString);
builder.Message("Form builder");
builder.Field(nameof(ModelClass.Title), active: (state) => false);
builder.Field(nameof(ModelClass.URL), active: (state) => false);
foreach(string param in json.Parameters)
{
builder.Field(param);
}
return builder.Build();
}
JSONstring 非常动态,每次都可能不同。但是,字符串始终包含“d”和“parameter”子节点。该字符串可能如下所示:
"{
\n\t\"d\": {
\n\t\t\"parameters\": [
{
\n\t\t\t\"id\": \"url\",
\n\t\t\t\"name\": \"Site URL\",
\n\t\t\t\"required\": \"text\"
},
{
\n\t\t\t\"id\": \"title\",
\n\t\t\t\"URL\": \"Title\",
\n\t\t\t\"required\": true,
\n\t\t\t\"example\": \"www.stackoverflow.com\"\n\t\t
}
]\n\t
}\n
}"
如何确保无论 JSON 是什么样子,参数都作为字段输入动态添加到表单构建器中?提前致谢。