2016-12-11 8 views
0

getからページが返されると検証がトリガーされる.NETコアアプリケーションがあります。私はビューモデルを持っていますが、応答を受け取ったらなぜ検証が既に起動されているのかわかりません。asp検証 - get .netコアでトリガーする

は、ここに私のモデルとマークアップ

using System.ComponentModel.DataAnnotations; 

    namespace MusicianProject.Models.ViewModels 
    { 
     public class RegisterViewModel 
     { 
      [Required(ErrorMessage ="First name is required.")] 
      [Display(Name ="First Name")] 
      [StringLength(25, ErrorMessage = "First name must be less than 25 characters.")] 
      public string FirstName { get; set; } 

      [Required(ErrorMessage = "Last name is required")] 
      [Display(Name = "Last Name")] 
      [StringLength(50, ErrorMessage = "Last name must be less than 50 characters.")] 
      public string LastName { get; set; } 

      [Required] 
      [EmailAddress] 
      [Display(Name = "Email")] 
      public string Email { get; set; } 

      [Required] 
      [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] 
      [DataType(DataType.Password)] 
      [Display(Name = "Password")] 
      public string Password { get; set; } 

      [Required] 
      [DataType(DataType.Password)] 
      [Display(Name = "Confirm password")] 
      [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
      public string ConfirmPassword { get; set; } 

     } 
    } 

されており、ここで返されるレジスタ図です。

@model MusicianProject.Models.ViewModels.RegisterViewModel 

<div class="container"> 
    <form asp-controller="Account" asp-action="Register" method="post"> 

     <div class="col-md-4 col-md-offset-4"> 

      <div class="form-group"> 
       <label asp-for="FirstName"></label> 
       <input class="form-control" type="text" asp-for="FirstName" /> 
       <span asp-validation-for="FirstName">First name is required.</span> 
      </div> 


      <div class="form-group"> 
       <label asp-for="LastName"></label> 
       <input class="form-control" asp-for="LastName" /> 
       <span asp-validation-for="LastName">Last name is required.</span> 
      </div> 

      <div class="form-group"> 
       <label asp-for="Email"></label> 
       <input class="form-control" type="text" asp-for="Email" /> 
       <span asp-validation-for="Email">Email is required.</span> 
      </div> 

      <div class="form-group"> 
       <label asp-for="Password"></label> 
       <input asp-for="Password" type="password" id="password" class="form-control" /> 
       <span asp-validation-for="Password">Password is required.</span> 
      </div> 

      <div class="form-group"> 
       <label asp-for="ConfirmPassword"></label> 
       <input asp-for="ConfirmPassword" type="password" id="confirm-password" class="form-control" /> 
       <span asp-validation-for="ConfirmPassword">Confirm password is required</span> 
      </div> 


      <div class="btn-group text-center"> 
       <button class="btn btn-default">Sign up!</button> 
       <button class="btn btn-danger">Cancel</button> 
      </div> 
     </div> 

    </form> 
</div> 

私は2つの登録メソッドpostとgetを持っています。ここに私が持っているものがあります。

[HttpGet] 
     public IActionResult Register() 
     { 
      return View(); 
     } 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public IActionResult Register(RegisterViewModel rvm) 
     { 
      if (ModelState.IsValid) 
      { 

       return RedirectToAction("Index", "Home"); 
      } 

      return View(); 
     } 

私のブラウザでルートに移動すると、ここに私が得るものがあります。

My signup form

答えて

1

単にあなたのビューで<span asp-validation-for>タグ内のすべてのテキストを削除します。

注釈が設定した内容に応じて、検証エラーが発生したときにタグヘルパーがテキストを挿入します。あなたが今それらのメッセージを強制的に印刷する方法です。

+0

私はそれをやろうとしましたが、私はこれらのスパンにクラスを置いて赤くしても、彼らはまだ見せています。私はブートストラップを使用しているので、私がそれらを置いているクラスはテキスト危険であり、まだ赤いアウトラインを示しています。データアノテーションを使用してクラスを制御する方法はありますか? – ddeamaral

+1

@ destructi6nあなたが変更した後の 'span'のようなものの例を私に見せてもらえますか? –

+0

このように見えます。 '' – ddeamaral

関連する問題