Form Elements

Introduction

Abp provides form input tag helpers to make building forms easier.

Demo

See the form elements demo page to see it in action.

abp-input

abp-input tag creates a Bootstrap form input for a given c# property. It uses Asp.Net Core Input Tag Helper in background, so every data annotation attribute of input tag helper of Asp.Net Core is also valid for abp-input.

Usage:

  1. <abp-input asp-for="@Model.MyModel.Name"/>
  2. <abp-input asp-for="@Model.MyModel.Description"/>
  3. <abp-input asp-for="@Model.MyModel.Password"/>
  4. <abp-input asp-for="@Model.MyModel.IsActive"/>

Model:

  1. public class FormElementsModel : PageModel
  2. {
  3. public SampleModel MyModel { get; set; }
  4. public void OnGet()
  5. {
  6. MyModel = new SampleModel();
  7. }
  8. public class SampleModel
  9. {
  10. [Required]
  11. [Placeholder("Enter your name...")]
  12. [InputInfoText("What is your name?")]
  13. public string Name { get; set; }
  14. [Required]
  15. [FormControlSize(AbpFormControlSize.Large)]
  16. public string SurName { get; set; }
  17. [TextArea(Rows = 4)]
  18. public string Description { get; set; }
  19. [Required]
  20. [DataType(DataType.Password)]
  21. public string Password { get; set; }
  22. public bool IsActive { get; set; }
  23. }
  24. }

Attributes

You can set some of the attributes on your c# property, or directly on html tag. If you are going to use this property in a abp-dynamic-form, then you can only set these properties via property attributes.

Property Attributes

  • [TextArea()]: Converts the input into a text area.

  • [Placeholder()]: Sets placeholder for input. You can use a localization key directly.

  • [InputInfoText()]: Sets a small info text for input. You can use a localization key directly.
  • [FormControlSize()]: Sets size of form-control wrapper element. Available values are
    • AbpFormControlSize.Default
    • AbpFormControlSize.Small
    • AbpFormControlSize.Medium
    • AbpFormControlSize.Large
  • [DisabledInput] : Input is disabled.
  • [ReadOnlyInput]: Input is read-only.

Tag Attributes

  • info: Sets a small info text for input. You can use a localization key directly.
  • auto-focus: If true, browser auto focuses on the element.
  • size: Sets size of form-control wrapper element. Available values are
    • AbpFormControlSize.Default
    • AbpFormControlSize.Small
    • AbpFormControlSize.Medium
    • AbpFormControlSize.Large
  • disabled: Input is disabled.
  • readonly: Input is read-only.
  • label: Sets the label for input.
  • display-required-symbol: Adds the required symbol (*) to label if input is required. Default True.

asp-format, name and value attributes of Asp.Net Core Input Tag Helper are also valid for abp-input tag helper.

Label & Localization

You can set label of your input in different ways:

  • You can use Label attribute and directly set the label. But it doesn’t auto localize your localization key. So use it as label="@L["{LocalizationKey}"].Value".
  • You can set it using [Display(name="{LocalizationKey}")] attribute of Asp.Net Core.
  • You can just let abp find the localization key for the property. It will try to find “DisplayName:{PropertyName}” or “{PropertyName}” localization keys, if label or [DisplayName] attributes are not set.

abp-select

abp-select tag creates a Bootstrap form select for a given c# property. It uses Asp.Net Core Select Tag Helper in background, so every data annotation attribute of select tag helper of Asp.Net Core is also valid for abp-select.

abp-select tag needs a list of Microsoft.AspNetCore.Mvc.Rendering.SelectListItem to work. It can be provided by asp-items attriube on the tag or [SelectItems()] attribute on c# property. (if you are using abp-dynamic-form, c# attribute is the only way.)

abp-select supports multiple selection.

abp-select auto-creates a select list for Enum properties. No extra data is needed. If property is nullable, an empty key and value is added to top of the auto-generated list.

Usage:

  1. <abp-select asp-for="@Model.MyModel.City" asp-items="@Model.CityList"/>
  2. <abp-select asp-for="@Model.MyModel.AnotherCity"/>
  3. <abp-select asp-for="@Model.MyModel.MultipleCities" asp-items="@Model.CityList"/>
  4. <abp-select asp-for="@Model.MyModel.MyCarType"/>
  5. <abp-select asp-for="@Model.MyModel.MyNullableCarType"/>

Model:

  1. public class FormElementsModel : PageModel
  2. {
  3. public SampleModel MyModel { get; set; }
  4. public List<SelectListItem> CityList { get; set; }
  5. public void OnGet()
  6. {
  7. MyModel = new SampleModel();
  8. CityList = new List<SelectListItem>
  9. {
  10. new SelectListItem { Value = "NY", Text = "New York"},
  11. new SelectListItem { Value = "LDN", Text = "London"},
  12. new SelectListItem { Value = "IST", Text = "Istanbul"},
  13. new SelectListItem { Value = "MOS", Text = "Moscow"}
  14. };
  15. }
  16. public class SampleModel
  17. {
  18. public string City { get; set; }
  19. [SelectItems(nameof(CityList))]
  20. public string AnotherCity { get; set; }
  21. public List<string> MultipleCities { get; set; }
  22. public CarType MyCarType { get; set; }
  23. public CarType? MyNullableCarType { get; set; }
  24. }
  25. public enum CarType
  26. {
  27. Sedan,
  28. Hatchback,
  29. StationWagon,
  30. Coupe
  31. }
  32. }

Attributes

You can set some of the attributes on your c# property, or directly on html tag. If you are going to use this property in a abp-dynamic-form, then you can only set these properties via property attributes.

Property Attributes

  • [SelectItems()]: Sets the select data. Parameter should be the name of the data list. (see example above)

  • [InputInfoText()]: Sets a small info text for input. You can use a localization key directly.

  • [FormControlSize()]: Sets size of form-control wrapper element. Available values are
    • AbpFormControlSize.Default
    • AbpFormControlSize.Small
    • AbpFormControlSize.Medium
    • AbpFormControlSize.Large

Tag Attributes

  • asp-items: Sets the select data. This Should be a list of SelectListItem.
  • info: Sets a small info text for input. You can use a localization key directly.
  • size: Sets size of form-control wrapper element. Available values are
    • AbpFormControlSize.Default
    • AbpFormControlSize.Small
    • AbpFormControlSize.Medium
    • AbpFormControlSize.Large
  • label: Sets the label for input.
  • display-required-symbol: Adds the required symbol (*) to label if input is required. Default True.

Label & Localization

You can set label of your input in different ways:

  • You can use Label attribute and directly set the label. But it doesn’t auto localize your localization key. So use it as label="@L["{LocalizationKey}"].Value".
  • You can set it using [Display(name="{LocalizationKey}")] attribute of Asp.Net Core.
  • You can just let abp find the localization key for the property. It will try to find “DisplayName:{PropertyName}” or “{PropertyName}” localization keys.

Localizations of combobox values are set by abp-select for Enum property. It searches for “{EnumTypeName}.{EnumPropertyName}” or “{EnumPropertyName}” localization keys. For instance, in the example above, it will use “CarType.StationWagon” or “StationWagon” keys for localization when it localizes combobox values.

abp-radio

abp-radio tag creates a Bootstrap form radio group for a given c# property. Usage is very similar to abp-select tag.

Usage:

  1. <abp-radio asp-for="@Model.MyModel.CityRadio" asp-items="@Model.CityList" inline="true"/>
  2. <abp-radio asp-for="@Model.MyModel.CityRadio2"/>

Model:

  1. public class FormElementsModel : PageModel
  2. {
  3. public SampleModel MyModel { get; set; }
  4. public List<SelectListItem> CityList { get; set; } = new List<SelectListItem>
  5. {
  6. new SelectListItem { Value = "NY", Text = "New York"},
  7. new SelectListItem { Value = "LDN", Text = "London"},
  8. new SelectListItem { Value = "IST", Text = "Istanbul"},
  9. new SelectListItem { Value = "MOS", Text = "Moscow"}
  10. };
  11. public void OnGet()
  12. {
  13. MyModel = new SampleModel();
  14. MyModel.CityRadio = "IST";
  15. MyModel.CityRadio2 = "MOS";
  16. }
  17. public class SampleModel
  18. {
  19. public string CityRadio { get; set; }
  20. [SelectItems(nameof(CityList))]
  21. public string CityRadio2 { get; set; }
  22. }
  23. }

Attributes

You can set some of the attributes on your c# property, or directly on html tag. If you are going to use this property in a abp-dynamic-form, then you can only set these properties via property attributes.

Property Attributes

  • [SelectItems()]: Sets the select data. Parameter should be the name of the data list. (see example above)

Tag Attributes

  • asp-items: Sets the select data. This Should be a list of SelectListItem.
  • Inline: If true, radio buttons will be in single line, next to each other. If false, they will be under each other.