Lookups, parents and row actions
Lookups: foreign keys as choices
[Lookup(typeof(CategoryModel)), Field(Label = "Category"), Required]
public int CategoryId { get; set; }
[Grid(Title = "Category")]
public string? CategoryName { get; set; } // flattened for the list
- The schema tells UIs the field is a choice from the
categoriespage. GET /api/crud/categories/lookup?q=too&take=20returns[{ "value": 3, "text": "Tools" }]: only rows the caller may see (their tenant, the page’s fixed filters, not soft-deleted).ids=3,4labels existing values.- Saving checks the value: a key that doesn’t exist, belongs to another tenant or is deleted is a 400, so a request can’t link to rows its caller can’t see.
[Lookup(typeof(X), Preload = true)]puts all options in the schema (small lists).
The text is the target page’s LookupText. In server-side code, ICrudLookups gives the same options, with the same visibility rules, e.g. for an MVC dropdown:
var items = await lookups.GetAsync<CategoryModel>(ct); // ICrudLookups
ViewBag.Categories = items.ToSelectList(selected: model.CategoryId, placeholder: "Choose…");
Parents: master-detail pages
[CrudPage("package-courses", Entity = typeof(PackageCourse))]
public class PackageCourseModel
{
public int Id { get; set; }
[Parent(typeof(PackageModel)), Field]
public int PackageId { get; set; }
...
}
- The parent page gets a row action that opens the children:
/package-courses?f.packageId=3. - Listing children needs the parent filter (
RequiredForList = falseto allow listing all). - A row can’t move to another parent: changing the parent key on update is a 400.
- The schema lists
parentandchildren, so UIs link them without configuration.
Row actions
[RowAction("invoice", Label = "Invoice", Icon = "receipt", Navigate = "/invoices/{id}", EnabledBy = "canInvoice")]
public class OrderModel
{
[Computed] public bool CanInvoice { get; set; }
}
Navigate fills {field} from the row; EnabledBy names a boolean field that enables the action per row. Edit and delete are built-in actions; EnabledBy = "canDelete" style flags come from computed values.