-
-
Notifications
You must be signed in to change notification settings - Fork 10
Controller model binding
A view model is an object parsed from the request body (for example, a JSON string) or the query string.
- The view model can be accessed via the controller's
T Model
property; - To access the view model asynchronously, first, you should call the
ReadModelAsync
method; otherwise, the model will be accessed synchronously on the firstT Model
access. - The view model will be parsed from the request only on the first
ReadModelAsync
call or on the firstT Model
access.
To work with view models, you should use Controller2<T>
, Controller<T>
, or AsyncController<T>
base classes for your controller, and then specify a model type in the type parameter T
.
public class LoginViewModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
public bool RememberMe { get; set; }
}
public class LoginController : AsyncController<LoginViewModel>
{
public override async Task<ControllerResponse> Invoke()
{
await ReadModelAsync();
// Accessing serialized model
if (Model.Password == ... && Model.UserName == ...)
{
...
}
...
}
}
<form method="post">
<input type="text" name="UserName" />
<input type="Password" name="Password" />
<input type="checkbox" name="RememberMe"/> Remember me
<button type="submit">Login</button>
</form>
Note that the model serialization and validation will be executed only on the first ReadModelAsync
call or Model
property access.
By default, the view model will be serialized from the HTTP GET query string or the HTTP POST form data (depending on the current request method). For a JSON type of data, you can use the JSON serializer.
Supported view model property types by default: string
, bool
, bool?
, int
, int?
, decimal
, decimal?
, DateTime
, DateTime?
, long
, long?
.
For DateTime
serialization, you can use the DateTimeFormat
attribute.
With this attribute, you can specify the DateTime
source data format, for example: [DateTimeFormat("dd.MM.yyyy Hh:mm")]
All validation attributes can be found in the Simplify.Web.ModelBinding.Attributes
namespace.
Available property validation attributes by default:
-
[Required]
- indicates that the field is required (can be set for any supported field types); -
[MaxLength(5)]
- specifies string property maximum length; -
[MinLength(2)]
- specifies string property minimum length; -
[EMailAttribute(2)]
- string property should be a valid e-mail address; -
[Regex(^[a-zA-Z]+$)]
- string property should match the specified regular expression; -
[Max(5)]
- specifies numerical property maximum value; -
[Min(5)]
- specifies numerical property minimum value; -
[Range(1, 5)]
- specifies numerical property range of allowed values;
All validation attributes specified above support custom error messages, either directly from the string table or by specifying a string.
When specifying a custom error message, by default it will try to load the string from StringTable
by its label ID, for example:
public class LoginViewModel
{
[Required("NotifyUserNameIsRequired")]
public string UserName { get; set; }
}
<?xml version="1.0" encoding="utf-8" ?>
<items>
<item name="NotifyUserNameIsRequired" value="User name is required" />
</items>
If you want to specify an error message without StringTable
, you should set the second attribute parameter isMessageFromStringTable
to false, for example:
public class LoginViewModel
{
[Required("User name is required", false)]
public string UserName { get; set; }
}
You can create your own custom validation attribute by inheriting from Simplify.Web.Model.Validation.Attributes.ValidationAttribute
.
For validation, in the overridden Validate
method, you should throw an exception if the object value
parameter is invalid.
- Getting Started
- Main Simplify.Web principles
- Simplify.Web controllers
- Simplify.Web views
- Simplify.Web templates
- Simplify.Web configuration
- Templates variables
- Static content
- Template factory
- Data collector
- String table
- File reader
- Web context
- Environment
- Dynamic environment
- Language manager
- Redirector
- HTML