动态表单

提示: 在开始阅读本文档之前,请确保你已经看过并理解了abp表单元素文档.

介绍

abp-dynamic-form 为给定c#模型创建bootstrap表单.

基本用法:

  1. <abp-dynamic-form abp-model="@Model.MyDetailedModel"/>

Model:

  1. public class DynamicFormsModel : PageModel
  2. {
  3. [BindProperty]
  4. public DetailedModel MyDetailedModel { get; set; }
  5. public List<SelectListItem> CountryList { get; set; } = new List<SelectListItem>
  6. {
  7. new SelectListItem { Value = "CA", Text = "Canada"},
  8. new SelectListItem { Value = "US", Text = "USA"},
  9. new SelectListItem { Value = "UK", Text = "United Kingdom"},
  10. new SelectListItem { Value = "RU", Text = "Russia"}
  11. };
  12. public void OnGet()
  13. {
  14. MyDetailedModel = new DetailedModel
  15. {
  16. Name = "",
  17. Description = "Lorem ipsum dolor sit amet.",
  18. IsActive = true,
  19. Age = 65,
  20. Day = DateTime.Now,
  21. MyCarType = CarType.Coupe,
  22. YourCarType = CarType.Sedan,
  23. Country = "RU",
  24. NeighborCountries = new List<string>() { "UK", "CA" }
  25. };
  26. }
  27. public class DetailedModel
  28. {
  29. [Required]
  30. [Placeholder("Enter your name...")]
  31. [Display(Name = "Name")]
  32. public string Name { get; set; }
  33. [TextArea(Rows = 4)]
  34. [Display(Name = "Description")]
  35. [InputInfoText("Describe Yourself")]
  36. public string Description { get; set; }
  37. [Required]
  38. [DataType(DataType.Password)]
  39. [Display(Name = "Password")]
  40. public string Password { get; set; }
  41. [Display(Name = "Is Active")]
  42. public bool IsActive { get; set; }
  43. [Required]
  44. [Display(Name = "Age")]
  45. public int Age { get; set; }
  46. [Required]
  47. [Display(Name = "My Car Type")]
  48. public CarType MyCarType { get; set; }
  49. [Required]
  50. [AbpRadioButton(Inline = true)]
  51. [Display(Name = "Your Car Type")]
  52. public CarType YourCarType { get; set; }
  53. [DataType(DataType.Date)]
  54. [Display(Name = "Day")]
  55. public DateTime Day { get; set; }
  56. [SelectItems(nameof(CountryList))]
  57. [Display(Name = "Country")]
  58. public string Country { get; set; }
  59. [SelectItems(nameof(CountryList))]
  60. [Display(Name = "Neighbor Countries")]
  61. public List<string> NeighborCountries { get; set; }
  62. }
  63. public enum CarType
  64. {
  65. Sedan,
  66. Hatchback,
  67. StationWagon,
  68. Coupe
  69. }
  70. }

Demo

参阅 动态表单demo页面查看示例.

Attributes

abp-model

为动态表单设置c#模型,模型的属性以表单形式转化为输入.

submit-button

可以为 TrueFalse.

如果为 True,则会在表单底部生成一个提交按钮.

默认值是 False.

required-symbols

可以为 TrueFalse.

如果为 True,则必需的输入将带有一个符号(*),表示它们是必需的.

默认值是 True.

表单内容布局

默认情况下,“abp-dynamic-form 会清除内部html并将inputs放入自身. 如果要向动态表单添加其他内容或将inputs放置到某些特定区域,可以使用<abp-form-content />标签. 这个标签将被表单内容替换, 而 abp-dynamic-form 标签的内部html的其余部分将保持不变.

用法:

  1. <abp-dynamic-form abp-model="@Model.MyExampleModel">
  2. <div>
  3. Some content....
  4. </div>
  5. <div class="input-area">
  6. <abp-form-content />
  7. </div>
  8. <div>
  9. Some more content....
  10. </div>
  11. </abp-dynamic-form>

输入排序

abp-dynamic-form 通过 DisplayOrder attribute对属性进行排序,然后按模型类中的属性顺序进行排序.

默认每个属性的 DisplayOrder attribute值是10000.

参见以下示例:

  1. public class OrderExampleModel
  2. {
  3. [DisplayOrder(10004)]
  4. public string Name{ get; set; }
  5. [DisplayOrder(10005)]
  6. public string Surname{ get; set; }
  7. //Default 10000
  8. public string EmailAddress { get; set; }
  9. [DisplayOrder(10003)]
  10. public string PhoneNumber { get; set; }
  11. [DisplayOrder(9999)]
  12. public string City { get; set; }
  13. }

在这个示例中,inputs字段顺序为: City > EmailAddress > PhoneNumber > Name > Surname.

忽略属性

默认情况下, abp-dynamic-form 会为模型类中的每个属性生成输入. 如果要忽略属性请使用 DynamicFormIgnore attribute.

参见以下示例:

  1. public class MyModel
  2. {
  3. public string Name { get; set; }
  4. [DynamicFormIgnore]
  5. public string Surname { get; set; }
  6. }

在这个示例中,不会为 Surname 属性生成输入.

指示文本框,单选按钮组和组合框

如果你已经阅读了表单元素文档,你会注意到在c#模型上 abp-radioabp-select 标签非常相. 我们必须使用 [AbpRadioButton()] attribute来告诉 abp-dynamic-form 你的哪些属性是单选按钮组,哪些属性是组合框.

参见以下示例:

  1. <abp-dynamic-form abp-model="@Model.MyDetailedModel"/>

Model:

  1. public class DynamicFormsModel : PageModel
  2. {
  3. [BindProperty]
  4. public DetailedModel MyDetailedModel { get; set; }
  5. public List<SelectListItem> CountryList { get; set; } = new List<SelectListItem>
  6. {
  7. new SelectListItem { Value = "CA", Text = "Canada"},
  8. new SelectListItem { Value = "US", Text = "USA"},
  9. new SelectListItem { Value = "UK", Text = "United Kingdom"},
  10. new SelectListItem { Value = "RU", Text = "Russia"}
  11. };
  12. public void OnGet()
  13. {
  14. MyDetailedModel = new DetailedModel
  15. {
  16. ComboCarType = CarType.Coupe,
  17. RadioCarType = CarType.Sedan,
  18. ComboCountry = "RU",
  19. RadioCountry = "UK"
  20. };
  21. }
  22. public class DetailedModel
  23. {
  24. public CarType ComboCarType { get; set; }
  25. [AbpRadioButton(Inline = true)]
  26. public CarType RadioCarType { get; set; }
  27. [SelectItems(nameof(CountryList))]
  28. public string ComboCountry { get; set; }
  29. [AbpRadioButton()]
  30. [SelectItems(nameof(CountryList))]
  31. public string RadioCountry { get; set; }
  32. }
  33. public enum CarType
  34. {
  35. Sedan,
  36. Hatchback,
  37. StationWagon,
  38. Coupe
  39. }
  40. }

正如你上面的例子中看到:

  • 如果在Enum属性上使用 [AbpRadioButton()],它将是一个单选按钮组. 否则它是组合框.
  • 如果在属性上使用 [SelectItems()][AbpRadioButton()],那么它将是一个单选按钮组.
  • 如果只在属性上使用 [SelectItems()],它将是一个组合框.
  • 如果一个属性没有使用这些属性,它将是一个文本框.

本地化

abp-dynamic-form 会处理本地化.

默认情况下, 它将尝试查找 “DisplayName:{PropertyName}” 或 “{PropertyName}” 定位本地化键,并将定位值设置为label.

你可以使用Asp.Net Core的 [Display()] attribute自行设置. 可以在此属性中使用本地化密钥. 请参阅以下示例:

  1. [Display(Name = "Name")]
  2. public string Name { get; set; }