Thursday, May 30, 2019

Model Validation Example in Asp.Net Core

In the previous post we saw on how to perform form field validations using Model State, in this post we will see an example of using Model Validations in a MVC Form. Following is the Model class with the validations.

    public partial class Users
    {
        public int UserId { get; set; }

        [StringLength(50)]
        [Required(ErrorMessage = "Please enter First Name.")]
        public string FirstName { get; set; }

        [StringLength(50)]
        [Required(ErrorMessage = "Please enter Last Name.")]
        public string LastName { get; set; }

        [StringLength(10)]
        [Required]
        [RegularExpression(@"^[0-9]{10}", ErrorMessage ="Only Numbers allowed.")]   
        public string Phone { get; set; }

        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
            ErrorMessage = "Please enter a valid Email address."),
            Required,
            StringLength(50)]
        public string Email { get; set; }

        [Required, StringLength(100)]
        public string Address { get; set; }

        public string AttachmentPath { get; set; }
    } 

As we see in the above model, we can either use the validation attributes separately or combine them in a single attribute. In the form we need to map the Model Attributes as follows.

        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Phone" class="control-label"></label>
                <input asp-for="Phone" class="form-control" />
                <span asp-validation-for="Phone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Email" class="control-label"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Address" class="control-label"></label>
                <input asp-for="Address" class="form-control" />
                <span asp-validation-for="Address" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>

Build the project and open the page, try to click the Create button without entering any values, it should display the validation errors as follows.




Search Flipkart Products:
Flipkart.com

1 comment:

Anonymous said...

Pada contoh di atas karena nilai dari kartu bandar 1 maka pemain yang juga bernilai 1 membayar jumlah taruhan kepada bandar
asikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terbaik
hobiqq
paito warna
bocoran sgp
data hk