Project model-state errors into view models¶
Translate ASP.NET model-state validation errors into JSON-friendly, grid-row-correlated view models. Trigger: Controller action returning validation results, typically as JSON.
| Field | Value |
|---|---|
| Operation ID | OP-16 |
| Trigger | Controller action returning validation results, typically as JSON |
| Inputs | ModelStateDictionary, optional grid property and row-id expressions |
| Outputs | ErrorViewModel[] |
| Primary components | ModelStateExtensions.GetErrors overload family (GetErrors(ModelStateDictionary), GetErrors<TGridRow, TProperty>(ModelStateDictionary, Expression, Expression), GetErrors<TGridRow, TProperty>(ModelStateDictionary, params GridErrorArguments<TGridRow, TProperty>[])) |
| Entry point | src/Web.Mvc/Extensions/ModelStateExtensions.cs |
Steps¶
GetErrors()(no arguments) uses a grid-row-id selector that always returns"", disabling grid correlation.- The grid overloads build
GridErrorArgumentsfrom lambda expressions, capturing the grid property name and row-id property name throughExpressionHelper.GetExpressionText. - For each model-state key, the
gridRowKeyselector picks the first translator whoseGridNameprefixes the key and whose key does not contain.Added[,.Deleted[, or.Updated[— pending-change collections are excluded from grid error correlation (BR-34). GridRowKeynormalizes the key: truncate at the last]; when the key already targets the view-model path ({grid}.Rows[) use it directly if it ends with the row-id name, otherwise append.{rowIdName}; when it targets the domain-model path, insert.Rowsafter the grid name before applying the same rule (BR-35).GridRowIdresolves the normalized key back throughModelStateand reads itsAttemptedValueas the row identifier, or""when no grid key applies.- Emit one
ErrorViewModelper model-state entry that has errors, withKeyhaving.replaced by_for DOM friendliness,Valuesset to all error messages, andGridRowIdset to the resolved row id (BR-36). LoadValidationErrors(ModelStateDictionary, IValidationResult)is the inbound counterpart: it adds one model error perresult.Errorsentry usingPropertyNameandErrorMessage.
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start([Controller returns validation result])
Args[Build GridErrorArguments from lambdas]
Key[Select grid translator per model-state key]
Norm[Normalize key via GridRowKey]
RowId[Resolve GridRowId from ModelState]
Emit[Emit ErrorViewModel array]
EndOk([Return ErrorViewModel[]])
Start --> Args
Args --> Key
Key --> Norm
Norm --> RowId
RowId --> Emit
Emit --> EndOk
Failure and edge-case behavior¶
- No unexpected failure paths beyond argument-guard exceptions and delegated dependency behavior were identified for this operation.
Related documentation¶
Sources¶
src/Web.Mvc/Extensions/ModelStateExtensions.cs