Skip to main content

Model Binding

The editor is bound to a PageModel property with asp-for. The property carries [RichTextHtml(MaxTextLength = 500)], so the posted HTML is cleaned with the editor's own policy on the server, the 500-character limit is checked there too, and the editor picks the same limit up by itself.

// PageModel
[BindProperty]
[RichTextHtml(MaxTextLength = 500)]
public string? Body { get; set; }

public void OnPost()
{
    if (!ModelState.IsValid) return;   // over 500 characters of text
    // Body is already sanitized: no script, no event handlers, no javascript: links
}

// Razor Page
<form method="post">
    <richtextbox asp-for="Body" toolbar="default" height="300px" />
    <span asp-validation-for="Body"></span>
    <button type="submit">Submit</button>
</form>