2016-08-29 6 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 VIEW КОД:

@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

добавить Login и SignUp в одной форме

@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

Shady говорит, что вы добавили оба Войти и регистрационные формы в одной форме. Вам нужно сделать это в двух отдельных формах в качестве кода, предоставленного им –

+0

Спасибо @shady, он работает хорошо .. Можете ли вы предложить мне какие-либо ссылки или книги, чтобы узнать MVC –