Since you're using asp.net core, you'll have the option to validate you model by decorating the properties:
public class MyEmployee
{
[Required]
public string FirstName;
[Required]
public string LastName;
[Required]
public string Age;
[Required]
public string Phone;
[Required]
public string Gender;
}
if you have that, you can validate the model from within a HTTP action call.
public async Task<IActionResult> OnPostAsync(MyEmployee model)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
await yourUpdate();
return Ok();
}
Note: this only works when you're using the model binders, which are enabled by default in the MVC and API actions.
There are various validation attributes available. Adding for example StringLength gives you the option to validate also for a length > 0.
Various defaults and regex options are available as well.
It puts the validation close to your model and leave you with a nice clean ActionResult method.
Also see Microsoft docs
More build in attributes:
Built-in attributes
Here are some of the built-in validation attributes:
- [ValidateNever]: Indicates that a property or parameter should be excluded from validation.
- [CreditCard]: Validates that the property has a credit card format. Requires jQuery Validation Additional Methods.
- [Compare]: Validates that two properties in a model match.
- [EmailAddress]: Validates that the property has an email format.
- [Phone]: Validates that the property has a telephone number format.
- [Range]: Validates that the property value falls within a specified range.
- [RegularExpression]: Validates that the property value matches a specified regular expression.
- [Required]: Validates that the field isn't null. See [Required] attribute for details about this attribute's behavior.
- [StringLength]: Validates that a string property value doesn't exceed a specified length limit.
- [Url]: Validates that the property has a URL format.
- [Remote]: Validates input on the client by calling an action method on the server. See [Remote] attribute for details about this attribute's behavior.
There are tons of these, and you can even build them yourself as well.