2016-08-29 56 views
0

我使用兩種模式,登錄&在視圖中註冊模型。驗證所需模型

public class Login 
{ 
    [Required(ErrorMessage ="User ID Required.")] 
    public string UserID { get; set; } 
    [Required(ErrorMessage ="Password Required")] 
    public string Password { get; set; } 

} 

public class SignUp 
{ 
    [Required (ErrorMessage ="User ID Required")] 
    public string UserID { get; set; } 

    [Required (ErrorMessage ="Name Required")] 
    public string Name { get; set; } 

    [Required (ErrorMessage ="Mail ID Required")] 
    public string MailID { get; set; } 

    [Required(ErrorMessage ="Password Required")] 
    public string Password { get; set; } 

    [Required(ErrorMessage ="Confirm Password Required")] 
    [Compare (nameof(Password), ErrorMessage ="Password does not match")] 
    public string ConfirmPassword { get; set; }   
} 

Validates on Both Model

當我點擊登錄按鈕,這既證明了模型。如何分別驗證模型?在控制器

使用以下代碼

public ActionResult Index() 
{ 
    return View(); 
} 
[HttpPost] 
public ActionResult Index(string Command, Login Login) 
{ 
    if (Command == "SIGNUP") 
    { 
     return RedirectToAction("Contact"); 
    } 
    else 
    { 
     if (ModelState.IsValidField("USERID") && ModelState.IsValidField("PASSWORD")) 
     { 
      return RedirectToAction("About"); 
     }    
    } 
    return View(); 
} 

Index.cshtml視圖代碼:

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
<div class="HolderForm"> 
    <div class="col-md-6"> 
     <div class="form-horizontal"> 
      <h4>Login</h4> 
      <hr /> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.Login.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br> 
        @Html.ValidationMessageFor(o => o.Login.UserID,"", new {@class= "LoginValidation" }) 
       </div> 
      </div> 
      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.Login.Password, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD" } })<br> 
        @Html.ValidationMessageFor(o => o.Login.Password,"", new { @class = "LoginValidation" }) 
        @ViewBag.Posted 
       </div> 
      </div> 
      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        @ViewBag.Posted 
       </div> 
      </div> 
      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" 
          value="LOGIN" 
          id="btn_Login" 
          name="Command" 
          class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    </div> 
    <div class="col-md-6"> 
     <div class="form-horizontal"> 
      <h4>SignUp</h4> 
      <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger"  }) 
      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.UserID,"", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.Name, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "NAME" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.Name, "", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.MailID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "MAIL ID" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.MailID, "", new  { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.Password, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD", @type = "password" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.Password, "", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.ConfirmPassword, new {  htmlAttributes = new { @class = "LoginEntry", @placeholder = "CONFIRM PASSWORD",  @type = "password" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.ConfirmPassword, "", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
         <input type= "submit" value="SIGNUP"        
           id="btn_Login" 
           name="Command" 
           class="btn btn-default" /> 
        </div> 
       </div> 
      </div> 
     </div>  
    </div> 


} 

以上代碼位置上使用兩個不同的模型的視圖。

請幫助,並建議我在哪裏學習ASP.NET(初級)?

回答

3

你在一個窗體添加LoginSignUp

@using (Html.BeginForm()){ 
    ... 
} 

試試這個

<div class="HolderForm"> 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="col-md-6"> 
     <div class="form-horizontal"> 
      <h4>Login</h4> 
      <hr /> 

      <div class="form-group"> 
       <div class="col-md-10"> 
       @Html.EditorFor(o => o.Login.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br> 
       @Html.ValidationMessageFor(o => o.Login.UserID,"", new {@class= "LoginValidation" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-10"> 
       @Html.EditorFor(o => o.Login.Password, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD" } })<br> 
       @Html.ValidationMessageFor(o => o.Login.Password,"", new { @class = "LoginValidation" }) 
       @ViewBag.Posted 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       @ViewBag.Posted 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" 
         value="LOGIN" 
         id="btn_Login" 
         name="Command" 
         class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
</div> 
} 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="col-md-6"> 
     <div class="form-horizontal"> 
      <h4>SignUp</h4> 
      <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger"  }) 
      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.UserID,"", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.Name, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "NAME" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.Name, "", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.MailID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "MAIL ID" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.MailID, "", new  { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.Password, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD", @type = "password" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.Password, "", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-10"> 
        @Html.EditorFor(o => o.SignUp.ConfirmPassword, new {  htmlAttributes = new { @class = "LoginEntry", @placeholder = "CONFIRM PASSWORD",  @type = "password" } })<br> 
        @Html.ValidationMessageFor(o => o.SignUp.ConfirmPassword, "", new { @class = "LoginValidation" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type= "submit" value="SIGNUP"        
          id="btn_Login" 
          name="Command" 
          class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    </div>  
</div> 


} 
+0

是啊,我說這兩款車型相同的形式像你提到 –

+0

黑幕是說,你加入這兩種登錄並在單一表格上註冊表格。你需要以兩種不同的形式,如他給出的代碼 –

+0

謝謝@shady,它運行良好..你能建議我任何鏈接或書籍來學習MVC –