4. Model Binding
1. Form values: These are form values that go in the HTTP request
using the POST method. (including jQuery POST requests).
1. Route values: The set of route values provided by routing.
1. Query strings: The query string part of the URI. Model.IsValid
6. Model Binding Attributes
[BindRequired]: This attribute adds a model state error if binding cannot occur.
[BindNever]: Tells the model binder to never bind to this parameter.
[FromHeader], [FromQuery], [FromRoute], [FromForm]: Use these to specify the exact binding
source you want to apply.
[FromServices]: This attribute uses dependency injection to bind parameters from services.
[FromBody]: Use the configured formatters to bind data from the request body. The formatter is
selected based on content type of the request.
[ModelBinder]: Used to override the default model binder, binding source and name.
7. Input/Output Formatters
Three input formatters:
– JsonInputFormatter – based on JSON.NET
– XmlSerializerInputFormatter – based on XmlSerializer (in the box, but not registered by default)
– XmlDataContractSerializerInputFormatter – based on DataContractSerializer
services.AddMvc()
.AddXmlSerializerFormatters();
services.AddMvc().Configure<MvcOptions>(options =>
{
options.InputFormatters.RemoveAll(formatter =>
formatter.Instance.GetType() == typeof(XmlDataContractSerializerInputFormatter));
});
8. Input/Output Formatters
Six output formatters:
– JsonOutputFormatter – based on JSON.NET
– XmlSerializerOutputFormatter – based on XmlSerializer (in the box, but not registered by default)
– XmlDataContractSerializerOutputFormatter – based on DataContractSerializer
– TextPlainFormatter – used to force a string into a text/plain content type
– HttpNoContentOutputFormatter – used to force 204 status code for null action return
– HttpNotAcceptableOutputFormatter – used to force 406 status code if no appropriate formatter can
be selected to handle the request (in the box, but not registered by default)
9. Model Validation
[CreditCard]: Validates the property has a credit card format.
[Compare]: Validates two properties in a model match.
[EmailAddress]: Validates the property has an email format.
[Phone]: Validates the property has a telephone format.
[Range]: Validates the property value falls within the given range.
[RegularExpression]: Validates that the data matches the specified regular expression.
[Required]: Makes a property required.
[StringLength]: Validates that a string property has at most the given maximum length.
[Url]: Validates the property has a URL format.
15. Tag Helpers
@Html.Label("FirstName", "First Name:", new {@class="caption"})
<label class="caption" asp-for="FirstName">First Name</label>
<label class="caption" for="FirstName">Name First</label>
16. Using Tag Helpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers // Add all tag helpers
@removeTabHelper *, Microsoft.AspNetCore.Mvc.TagHelpers // Remove all tag helpers
@tagHelperPrefix th:
<!span asp-validation-for="Email" class="text-danger"></!span> // Disable tag helper
<th:label asp-validation-for="Email" class="text-danger"></th:label> // Use prefix
17. Tag Helpers In Forms
<form asp-controller="Demo" asp-action="Register" method="post">
<!-- Input and Submit elements -->
</form>
<form method="post" action="/Demo/Register">
<!-- Input and Submit elements -->
<input name="__RequestVerificationToken" type="hidden" value="<removed for
brevity>" />
</form>
18. Custom Tag Helper
public class EmailTagHelper : TagHelper
{
private const string EmailDomain = "contoso.com";
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "a"; // Replaces <email> with <a> tag
var content = await output.GetChildContentAsync();
var target = content.GetContent() + "@" + EmailDomain;
output.Attributes.SetAttribute("href", "mailto:" + target);
output.Content.SetContent(target);
}
}
<email>Support</email> <a href="mailto:Support@contoso.com">Support@contoso.com</a>