Getting Started with Asp.Net Core (X) - Model binding and validation

  When the map data model binding Http request to the parameter corresponding operating method on the controller, the operation process parameter may be a simple type, such as plastic, string, etc., may be a complex type, such as Product, Order like.

  Asp.Net Core MVC model binding and binding Asp.Net MVC model similar to the model binding sequence diagram pressed on the specified search parameters from the binding data of an http request to the controller corresponding method of operation.

  Meanwhile, when Asp.Net MVC Core binding model will also verify the model. So, how do we add a check to the model, in fact, and almost Asp.Net MVC.

  First we add validation attributes on the properties of the model, Display attributes to display on a page of the field information.

///  <the Summary> 
/// student model
 ///  </ the Summary> 
public  class Student
{
    public int Id { get; set; }

    [Display(Name="姓名")]
    [The Required (the ErrorMessage = " Please enter the name " )]
     public  String the Name { GET ; SET ;}

    [Display (the Name = " class " )]
    [The Required (the ErrorMessage = " Please enter the class " )]
     public ClassNameEnum ClassName {? GET ; SET ;}

    [Display (the Name = " email address " )]
    [The Required (the ErrorMessage = " Please enter the email address " )]
     public  String In Email { GET ; SET ;}
}

General properties check are:

  Required specifies that the field is required

  Range specified minimum and maximum allowable

  The minimum length specified string MinLength

  The maximum length of the string specified MaxLength

  Compare Comparative models two attributes, for example, Email, and comparing properties ComfirmEmail

  RegularExpression whether the value of the regular expression, to provide verification of the specified regular expression pattern matching

 

  Secondly, the use ModelState.IsValid property validation attribute is bound success

if (ModelState.IsValid)
{
    Student newStudent = _studentRepository.Add(student);

    return RedirectToAction("Details", new { id = newStudent.Id });
}
else
{
    return View(student);
}

  Finally, asp-validation-for and asp-validation-summary tag helper to display the error message

<div asp-validation-summary="All" class="text-danger"></div>

        <div class="form-group row">
            <label asp-for="Name" class="col-sm-2 col-form-label"></label>
            <div class="col-sm-10">
                <input asp-for="Name" class="form-control" placeholder="请输入名字" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
        </div>


        <div class="form-group row">
            <label asp-for="Email" class="col-sm-2 col-form-label"></label>
            <div class="col-sm-10">
                <input asp-for="Email" class="form-control" placeholder="请输入邮箱" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
        </div>


        <div class="form-group row">
            <label asp-for="ClassName" class="col-sm-2 col-form-label"></label>
            <div class="col-sm-10">
                <select asp-for="ClassName" asp-items="Html.GetEnumSelectList<ClassNameEnum>()">
                    <option value="" selected></option>
                </select>
                <span asp-validation-for="ClassName" class="text-danger"></span>
            </div>
        </div>

  It is noteworthy that, on the verification select label, the model has no Required Required and will prompt The value '' is invalid. This is because there is ClassName enumeration type int, and the value in the option is "", leading to type conversion failed, we can be empty type ClassNameEnum ClassName set in the Student?.

 

Guess you like

Origin www.cnblogs.com/jesen1315/p/11067225.html