如何在Mvc4中的一个视图中使用两个模型?模型值返回空值。 [英] How Do I Use Two Model In One View In Mvc4? Model Values Are Returning Null.

查看:95
本文介绍了如何在Mvc4中的一个视图中使用两个模型?模型值返回空值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MVC4中的一个视图中使用两个模型?

我想在我的表单中使用两个不同的模型。一个用于登录另一个用于登录。我想在主视图中使用这两个模型。我添加了一个模型,其中包含另外两个用于注册和登录的模型。但我无法获取控制器中这些模型的值。值将变为空。

请参阅我的模型代码:

How do i use two model in one view in MVC4?
I want to use two different models in my form.One is for registration another for login. I want to use both models in my main view. I added one model that contains two other model for registration and login. but am unable to fetch the values in controller for those models. values are coming as null.
See my code for model:

public class RegistrationModel
   {
       [Required(ErrorMessage="First name is required")]
       public string FirstName { get; set; }

       [Required(ErrorMessage="Last name is required")]
       public string LastName {get; set;}

       [Required(ErrorMessage="Email is required")]
       [RegularExpression("^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$", ErrorMessage = "Invalid email")]
       public string Email { get; set; }

       [Required(ErrorMessage="Age is required")]
       [RegularExpression("[0-9]{1,2}", ErrorMessage = "Invalid age")]
       public int Age { get; set; }

       [Required(ErrorMessage="Date of birth is required")]
       public string Dob { get; set; }

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

       [Required(ErrorMessage = "Confirm Password is required")]
       [Compare("Pwd",ErrorMessage="Password mismatch")]
       public string Cpwd { get; set; }


   }
   public class LoginModel
   {
       [Required(ErrorMessage = "User name is required")]
       [Display(Name = "User name")]
       [RegularExpression("^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$", ErrorMessage = "Invalid email")]
       public string UserName { get; set; }

       [Required(ErrorMessage = "Password is required")]
       public string Pwd { get; set; }
   }
   public class Account
       {
           public LoginModel LoginModel{get; set;}
           public RegistrationModel RegisterModel {get; set;}
       }







我认为代码:




Code in my view:

  @model Selfie.Models.Account
//Register
     @using (Html.BeginForm())     
        { 
            
           @Html.ValidationSummary(true)          
          <form  role="form">
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">First Name:</label>          
              <div class="col-sm-8">
                   @Html.TextBoxFor( model => model.RegisterModel.FirstName,null, new {@class="form-control",id="fn", placeholder=" First Name",required="true",maxlength="20"})
                   @Html.ValidationMessageFor(model => model.RegisterModel.FirstName) 
               <span id="erfn" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Last Name:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.LastName,null, new { @class = "form-control",id="ln", placeholder=" Last Name",required="true",maxlength="20"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.LastName)
              <span id="erln" style="color:red"></span> 
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Age:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.Age,null, new { @class = "form-control",id="ae", placeholder=" Age",required="true",maxlength="2"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.Age) 
              <span id="erae" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Email:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.Email,null, new { @class = "form-control",id="em", placeholder=" Email",required="true",data_error="Bruh, that email address is invalid",type="email",maxlength="30"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.Email) 
              <span id="erem" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.RegisterModel.Pwd,null, new { @class = "form-control",data_minlength="6",id="p",placeholder=" Password",required="true",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.RegisterModel.Pwd) 
              <span id="erpl" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Confirm Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.RegisterModel.Cpwd,null, new { @class = "form-control",id="cp",placeholder=" Confirm Password",required="true",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.RegisterModel.Cpwd) 
              <span id="erpm" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                  <label><input type="checkbox" id="chkter" required><a id="lt" href="#" data-toggle="modal" data-target="#myModalterms"> Agree terms and conditions</a> </label><br /><span id="erchk" style="color:red"></span>
              </div>
              </div>
         
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                   <button type="submit" class="btn btn-success" id="btnsub">Create Account</button>        
                   <button type="reset" class="btn btn-danger" id ="btnclr">Clear</button> 
              </div>
              </div>

             <div class="modal-footer">
             </div>
      </form>
        } 
//Login
@using (Html.BeginForm("Login", "Selfie", FormMethod.Post, new { }))
        {   
      
          @Html.ValidationSummary(true)       
         <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Email:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.LoginModel.UserName,null, new { @class = "form-control",id="lem", placeholder=" Email",maxlength="30"})
                    @Html.ValidationMessageFor(model => model.LoginModel.UserName) 
              <span id="lerem" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.LoginModel.Pwd,null, new { @class = "form-control",data_minlength="6",id="lp",placeholder=" Password",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.LoginModel.Pwd) 
              <span id="lerpl" style="color:red"></span>
              </div>
              </div>
         <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                    <button type="submit" class="btn btn-success" id="btnlogin" name="command" value="user">Log In</button>
                    <button type="reset" class="btn btn-danger" id="btnlclr">Clear</button>
              </div>
              </div>
          
        <div class="divsplit"></div>
         <div class="modal-footer">
             <button type="submit" class="btn btn-danger" name="command" value="google">Sign In using Google</button>
             <button type="submit" class="btn btn-primary" name="command" value="facebook">Sign In using Facebook</button>
         
         </div>
        }      





Code for control:





Code for control:

[HttpPost]
      public ActionResult Register(RegistrationModel registration)
      {
          // Here registration values are null even after am selecting values
      }

      [HttpPost]
      public ActionResult Login(LoginModel login)
      {

          // Here LoginModel values are null even after am selecting values
      }





CAn i use like

[HttpPost]

public ActionResult Login(AccountModel account)

{

string test = account.LoginModel.Username;

}will it work ?



CAn i use like
[HttpPost]
public ActionResult Login(AccountModel account)
{
string test = account.LoginModel.Username;
}will it work ?

推荐答案

\", ErrorMessage = \"Invalid email\")]
public string Email { get; set ; }

[Required(ErrorMessage=\"Age is required\")]
[RegularExpression(\"[0-9]{1,2}\", ErrorMessage = \"Invalid age\")]
public int Age { get; set ; }

[Required(ErrorMessage=\"Date of birth is required\")]
public string Dob { get; set ; }

[必需(ErrorMessage = 密码是必需的)]
public string Pwd { get ; set ; }

[Required(ErrorMessage = \"Confirm Password is required\")]
[Compare(\"Pwd\",ErrorMessage=\"Password mismatch\")]
public string Cpwd { get; set ; }


}
public class LoginModel
{
[Required(ErrorMessage = \"User name is required\")]
[Display(Name = \"User name\")]
[RegularExpression(\"^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)
", ErrorMessage = "Invalid email")] public string Email { get; set; } [Required(ErrorMessage="Age is required")] [RegularExpression("[0-9]{1,2}", ErrorMessage = "Invalid age")] public int Age { get; set; } [Required(ErrorMessage="Date of birth is required")] public string Dob { get; set; } [Required(ErrorMessage="Password is required")] public string Pwd { get; set; } [Required(ErrorMessage = "Confirm Password is required")] [Compare("Pwd",ErrorMessage="Password mismatch")] public string Cpwd { get; set; } } public class LoginModel { [Required(ErrorMessage = "User name is required")] [Display(Name = "User name")] [RegularExpression("^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)


\", ErrorMessage = \"Invalid email\")]
public string UserName { get; set ; }

[必需(ErrorMessage = 密码是必需的)]
public string Pwd { get ; set ; }
}
public class 帐户
{
public LoginModel LoginModel { get ; set ;}
public RegistrationModel RegisterModel { get ; set;}
}
", ErrorMessage = "Invalid email")] public string UserName { get; set; } [Required(ErrorMessage = "Password is required")] public string Pwd { get; set; } } public class Account { public LoginModel LoginModel{get; set;} public RegistrationModel RegisterModel {get; set;} }







Code in my view:




Code in my view:

  @model Selfie.Models.Account
//Register
     @using (Html.BeginForm())     
        { 
            
           @Html.ValidationSummary(true)          
          <form  role="form">
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">First Name:</label>          
              <div class="col-sm-8">
                   @Html.TextBoxFor( model => model.RegisterModel.FirstName,null, new {@class="form-control",id="fn", placeholder=" First Name",required="true",maxlength="20"})
                   @Html.ValidationMessageFor(model => model.RegisterModel.FirstName) 
               <span id="erfn" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Last Name:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.LastName,null, new { @class = "form-control",id="ln", placeholder=" Last Name",required="true",maxlength="20"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.LastName)
              <span id="erln" style="color:red"></span> 
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Age:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.Age,null, new { @class = "form-control",id="ae", placeholder=" Age",required="true",maxlength="2"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.Age) 
              <span id="erae" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Email:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.Email,null, new { @class = "form-control",id="em", placeholder=" Email",required="true",data_error="Bruh, that email address is invalid",type="email",maxlength="30"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.Email) 
              <span id="erem" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.RegisterModel.Pwd,null, new { @class = "form-control",data_minlength="6",id="p",placeholder=" Password",required="true",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.RegisterModel.Pwd) 
              <span id="erpl" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Confirm Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.RegisterModel.Cpwd,null, new { @class = "form-control",id="cp",placeholder=" Confirm Password",required="true",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.RegisterModel.Cpwd) 
              <span id="erpm" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                  <label><input type="checkbox" id="chkter" required><a id="lt" href="#" data-toggle="modal" data-target="#myModalterms"> Agree terms and conditions</a> </label><br /><span id="erchk" style="color:red"></span>
              </div>
              </div>
         
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                   <button type="submit" class="btn btn-success" id="btnsub">Create Account</button>        
                   <button type="reset" class="btn btn-danger" id ="btnclr">Clear</button> 
              </div>
              </div>

             <div class="modal-footer">
             </div>
      </form>
        } 
//Login
@using (Html.BeginForm("Login", "Selfie", FormMethod.Post, new { }))
        {   
      
          @Html.ValidationSummary(true)       
         <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Email:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.LoginModel.UserName,null, new { @class = "form-control",id="lem", placeholder=" Email",maxlength="30"})
                    @Html.ValidationMessageFor(model => model.LoginModel.UserName) 
              <span id="lerem" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.LoginModel.Pwd,null, new { @class = "form-control",data_minlength="6",id="lp",placeholder=" Password",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.LoginModel.Pwd) 
              <span id="lerpl" style="color:red"></span>
              </div>
              </div>
         <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                    <button type="submit" class="btn btn-success" id="btnlogin" name="command" value="user">Log In</button>
                    <button type="reset" class="btn btn-danger" id="btnlclr">Clear</button>
              </div>
              </div>
          
        <div class="divsplit"></div>
         <div class="modal-footer">
             <button type="submit" class="btn btn-danger" name="command" value="google">Sign In using Google</button>
             <button type="submit" class="btn btn-primary" name="command" value="facebook">Sign In using Facebook</button>
         
         </div>
        }      





Code for control:





Code for control:

[HttpPost]
      public ActionResult Register(RegistrationModel registration)
      {
          // Here registration values are null even after am selecting values
      }

      [HttpPost]
      public ActionResult Login(LoginModel login)
      {

          // Here LoginModel values are null even after am selecting values
      }





CAn i use like

[HttpPost]

public ActionResult Login(AccountModel account)

{

string test = account.LoginModel.Username;

}will it work ?



CAn i use like
[HttpPost]
public ActionResult Login(AccountModel account)
{
string test = account.LoginModel.Username;
}will it work ?


This article discusses many approaches to do this - Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^].

Multiple M odels in Single View in MVC[^] will also help you.
This article discusses many approaches to do this - Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^].
Multiple Models in Single View in MVC[^] will also help you.


这篇关于如何在Mvc4中的一个视图中使用两个模型?模型值返回空值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆