2017-12-31 33 views
0

すべてのテキストボックスを入力して[送信]ボタンをクリックしていない場合、モデルの状態エラーを追加してビューページに表示する方法(または)ビューページでこのエラーを表示する方法(すべてのフィールドは必須) asp.netでの知識は、私は最近、私はプロパティやモデルにカスタム検証を行う場合me.itはあなたに モデル状態エラーを追加してビューページに表示する方法は?


 

 
    @using StackApplication.Modelsl; 
 

 

 

 
<!DOCTYPE html> 
 

 
<html> 
 
<head> 
 
    <meta name="viewport" content="width=device-width" /> 
 
    <title>Register Page</title> 
 
</head> 
 

 
<body> 
 
    <div class="header"> 
 
     <div id="title"> 
 
      <font color="blue"> <b> <h1>STaCK</h1> </b> </font> 
 
     </div> 
 
     <div class="sub"> 
 
      <a href="" style="color:white; 
 
       padding:10px;">Log in</a> 
 
      <a href="" style="color:white;">Sign up</a> 
 
     </div> 
 
    </div> 
 

 

 
    <div style="float:left;width:100%;padding:left:10px;top:5px;"> 
 
     <center> 
 
      <b><i> <h2><font color="#010100">Register Form</font></h2> </i></b> 
 
     </center> 
 
    </div> 
 

 
    <div id="content"> 
 
     <center> 
 
      @using (Html.BeginForm(FormMethod.Post)) 
 
      { 
 
       <table cellpadding="5" cellspacing="10"> 
 
        <tr> 
 
         <th><h3>Username</h3></th> 
 
         <td> 
 
          @Html.TextBoxFor(m=>m.UserAccount.UserName, new { @placeholder = "Username", @id = "txtUsername", @required="required" }) 
 
         </td> 
 
        </tr> 
 
        <tr> 
 
         <th><h3>Email id</h3></th> 
 
         <td> 
 

 
          @Html.TextBoxFor(m => m.UserAccount.Email, new { @placeholder = "Email", @id = "txtEmail", @required = "required" }) 
 

 
          
 
         </td> 
 
        </tr> 
 
        <tr> 
 
         <th><h3>Password</h3></th> 
 
         <td> 
 
          
 
          @Html.TextBoxFor(m => m.UserAccount.Password, new { @placeholder = "Password", @id = "txtPassword", @required = "required", @type="password" })   
 
          
 
           
 
         </td> 
 
        </tr> 
 

 
       </table> 
 

 
       <input type="submit" name="register" value="REGISTER" id="buttondesign" /> 
 
       <br /> 
 
       <br /> 
 
      } 
 
    </div> 
 
</body> 
 
</html> 
 

public class StackProvider 
{ 
    public object ModelState { get; private set; } 

    public string CreateUserAccount(UserAccount userAccount) 
    { 
     try 
     { 
      StackRepository repository = new StackRepository(); 

      if (string.IsNullOrEmpty(userAccount.Email) || string.IsNullOrEmpty(userAccount.Password) 
       || string.IsNullOrEmpty(userAccount.UserName)) 
      { 
       return "All fields are mandatory."; 
      } 

      int count = repository.GetUseraccountByEmail(userAccount.Email); 

      if (count > 0) 
      { 
       ModelState.AddModelError("Username already exist"); 
       return "Username already exist"; 
      } 

      repository.CreateUserAccount(userAccount); 

      return "Success"; 
     } 
     catch (Exception ex) 
     { 
      return ex.Message; 
     } 
    } 
+0

[検証の追加](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/validation)をご覧ください。 – Shyju

答えて

0

をme..thankingため非常に困難でなければなりません助けてください、これを学びます私はこれが好きです、あなたの問題を見ましたので、これはフォームを提出するためにあなたを助けるかもしれません...

=>実行の最後にModelStateオブジェクトには、検証の失敗のために追加されたすべてのエラーが含まれます。 のmodelStateオブジェクトにもエラーメッセージが含まれます。

=>私たちは、IsValidプロパティの値をチェックされ、それが偽に設定されますので、私たちはフィールドを埋めるために、再びレジスタビューにユーザーをリダイレクトしていると。

  public ActionResult Register() 
      { 
      return View(); 


     } 
     [HttpPost] 
    public ActionResult Register(Register registermodel) 
     //Here we are checking wether input field are null or Empty 

    //if fields are Null/Empty ,we are adding error in ModelState object against those properties 
    { 
     if (string.IsNullOrEmpty(registermodel.UserName)) 
     { 
      ModelState.AddModelError("UserName","UserName is required"); 
     } 
     if (string.IsNullOrEmpty(registermodel.email)) 
     { 
      ModelState.AddModelError("email ", "email is required"); 
     } 
     if (string.IsNullOrEmpty(registermodel.Password)) 
     { 
      ModelState.AddModelError("Password ", "Password is required"); 
     } 



     //Here,if All the validation are passed then redirect to index page. 
     if (ModelState.IsValid) 
     { 

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

     //if we got this far,something failed,redisplay form 
     return View(registermodel); 
    } 
関連する問題