2017-12-31 16 views
0

사용자가 모든 텍스트 상자를 채우지 않고 제출 버튼을 클릭하지 않으면 모델 상태 오류를 추가하고보기 페이지에 표시하는 방법 (또는)보기 페이지에서이 오류를 표시하는 방법 (모든 필드는 필수 항목입니다) .i 좋지 않습니다. asp.net 지식, 나는 최근에이 내용 때문에, 모델 상태 오류를 추가하고보기 페이지에 표시하는 방법은 무엇입니까?


 

 
    @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 매우 어려운 me.it이어야 도와주세요 나는 이것을 좋아하지 만 문제를 봤기 때문에이 양식을 제출할 때 도움이 될 것입니다 ...

=> 실행 종료시 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); 
    } 
관련 문제