2017-05-24 24 views
0

yah複数のモデルを1つのビューで使用する方法はたくさんあります。そして、私はそこから一つの選択肢を選ぶ=>と=>複数のモデルを1つのビューで使用する.net Mvc

複合クラス=>

public class combo 
{ 
    public IEnumerable<User> User { get; set; } 
    public IEnumerable<StudentRegistrationViewModel> StudentRegistrationViewModel { get; set; } 
    public IEnumerable<StudentFullRegistrationViewModel> StudentFullRegistrationViewModel { get; set; } 
} 

私は=>(私のモデル)のようでしたが、余分なクラスを作成し、そのクラスに、これらの複数のクラスを追加することです

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.ComponentModel.DataAnnotations.Schema; 
    using System.Linq; 
    using System.Web; 

    namespace TSMS_Atp_2_Final_Project.Models.Com.Tsms 
    { 
     public class User 
     { 
      [Key] 
      [Required(AllowEmptyStrings = false, ErrorMessage = "User Id Is Required!")] 
      [MaxLength(12, ErrorMessage = "You Have Exceed The Max length Of User ID which is [12] character!")] 
      [RegularExpression("[1-3]{2}-[0-9]{5}-[123]{1}|[1-3]{2}-[0-9]{7}-[123]{1}", ErrorMessage = "Invalid Id,It should [xx]-[xxxxx]-[x] or [xx]-[xxxxxxx]-[x]!")] 
      [Display(Name = "User ID")] 
      public string UserId { get; set; } 
      /// <summary> 
      /// //////// 
      /// </summary> 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Password Is Required!")] 
      [MaxLength(20, ErrorMessage = "Password Max Length Is 20 Character!")] 
      [DataType(DataType.Password)] 
      [Display(Name = "Password")] 
      public string password { get; set; } 
      /// <summary> 
      /// //////// 
      /// </summary> 
      [MaxLength(100, ErrorMessage = "The Max Length For User Level Is 100 Character!")] 
      [RegularExpression("^(?:admin|Admin|student|Student)$", ErrorMessage = "Invalid User Level!")] 
      [Display(Name = "User Level")] 
      public string level { get; set; } 
      /// <summary> 
      /// ///////// 
      /// </summary> 
      [NotMapped] 
      [Display(Name="Remember Me")] 
      public bool RememberMe { get; set; } 

      //relationship with other table---- 
      public List<UserDetail> UserDetail { get; set; } 
     } 

     public class UserPassViewModel 
     { 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Old Password Is Required!")] 
      [MaxLength(20, ErrorMessage = "Old Password Max Length Is 20 Character!")] 
      [DataType(DataType.Password)] 
      [Display(Name = "Old Password")] 
      public string Oldpassword { get; set; } 
      /// <summary> 
      /// /////// 
      /// </summary> 
      [Required(AllowEmptyStrings = false, ErrorMessage = "New Password Is Required!")] 
      [MaxLength(20, ErrorMessage = "New Password Max Length Is 20 Character!")] 
      [DataType(DataType.Password)] 
      [Display(Name = "New Password")] 
      public string Newpassword { get; set; } 
      /// <summary> 
      /// //////// 
      /// </summary> 
      [Compare("Newpassword", ErrorMessage = "New Password And Confirm Password Is Not Matched!")] 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Confirm Password Is Required!")] 
      [MaxLength(20, ErrorMessage = "Confirm Password Max Length Is 20 Character!")] 
      [DataType(DataType.Password)] 
      [Display(Name = "Confirm Password")] 
      public string Confirmpassword { get; set; } 
     } 

     public class StudentRegistrationViewModel 
     { 
      [Required(AllowEmptyStrings = false, ErrorMessage = "First Name IS Required!")] 
      [MaxLength(50, ErrorMessage = "The Max Length Of First Name Is 50 Character!")] 
      [DataType(DataType.Text, ErrorMessage = "Invalid First Name!")] 
      [Display(Name="First Name")] 
      public string FirstName { get; set; } 
      /// <summary> 
      /// /////// 
      /// </summary> 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Last Name Is Required!")] 
      [MaxLength(50, ErrorMessage = "The Max Length Of Last Name is 50 Character!")] 
      [DataType(DataType.Text, ErrorMessage = "Invalid Last Name!")] 
      [Display(Name = "Last Name")] 
      public string LastName { get; set; } 
      /// <summary> 
      /// ////// 
      /// </summary> 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Full Name Is Required!")] 
      [MaxLength(100, ErrorMessage = "The Max Length Of Full Name Is 100 Character!")] 
      [DataType(DataType.Text, ErrorMessage = "Invalid Full Name!")] 
      [Display(Name = "Full Name")] 
      public string FullName { get; set; } 
      /// <summary> 
      /// ///// 
      /// </summary> 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Email Is Required!")] 
      [EmailAddress(ErrorMessage = "Invalid Email Address!")] 
      [Display(Name = "Email Address")] 
      public string Email { get; set; } 
      /// <summary> 
      /// ////// 
      /// </summary> 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Password Is Required!")] 
      [MaxLength(20, ErrorMessage = "Password Max Length Is 20 Character!")] 
      [DataType(DataType.Password)] 
      [Display(Name = "Password")] 
      public string Password { get; set; } 
      /// <summary> 
      /// ///////// 
      /// </summary> 
      [Compare("Password", ErrorMessage = "Password And Confirm Password Is Not Matched!")] 
      [Required(AllowEmptyStrings = false, ErrorMessage = "Confirm Password Is Required!")] 
      [MaxLength(20, ErrorMessage = "Confirm Password Max Length Is 20 Character!")] 
      [DataType(DataType.Password)] 
      [Display(Name = "Confirm Password")] 
      public string ConfirmPassword { get; set; } 
     } 

     public class StudentFullRegistrationViewModel 
     { 
      public String MyProperty { get; set; } 
     } 

     public class combo 
     { 
      public IEnumerable<User> User { get; set; } 
      public IEnumerable<StudentRegistrationViewModel> StudentRegistrationViewModel { get; set; } 
      public IEnumerable<StudentFullRegistrationViewModel> StudentFullRegistrationViewModel { get; set; } 
     } 
    } 

と私の見解=>

@using something 
    @model combo 

    @{ 
     ViewBag.Title = "User Login"; 
    } 
    <script type="text/javascript"> 
     $(function() { 
      $('#login-form-link').click(function (e) { 
       $("#login-form").delay(100).fadeIn(100); 
       $("#register-form").fadeOut(100); 
       $('#register-form-link').removeClass('active'); 
       $(this).addClass('active'); 
       e.preventDefault(); 
      }); 

      $('#register-form-link').click(function (e) { 
       $("#register-form").delay(100).fadeIn(100); 
       $("#login-form").fadeOut(100); 
       $('#login-form-link').removeClass('active'); 
       $(this).addClass('active'); 
       e.preventDefault(); 
      }); 
     }); 
    </script> 

    @if (ViewBag.Message != null) 
    { 
     @section Scripts{ 
      <script type="text/javascript"> 
       $(function() { 
        CustomMessage('Error', '@ViewBag.Message', 'Close'); 
       }); 
      </script> 
     } 
    } 

    <div class="container"> 
     <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
       <div class="panel panel-login"> 
        <div class="panel-heading"> 
         <div class="row"> 
          <div class="col-xs-6"> 
           <a href="#" class="active" id="login-form-link">Login</a> 
          </div> 
          <div class="col-xs-6"> 
           <a href="#" id="register-form-link">Register</a> 
          </div> 
         </div> 
         <hr> 
        </div> 
        <div class="panel-body container-fluid"> 
         <div class="row"> 
          <div class="col-lg-12"> 
           @*login form*@ 
           <form id="login-form" method="post" role="form" style="display: block;"> 
            @Html.AntiForgeryToken() 
            @Html.ValidationSummary(true, null, new { @class = "text-danger" }) 
            <div class="form-group"> 
             @Html.TextBoxFor(model => model.StudentFullRegistrationViewModel., new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.UserId) }) 
             @Html.ValidationMessageFor(model => model.UserId, null, new { @class = "text-danger" }) 
            </div> 
            <div class="form-group"> 
             @Html.PasswordFor(model => model.password, new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.password) }) 
             @Html.ValidationMessageFor(model => model.password, null, new { @class = "text-danger" }) 
            </div> 
            <div class="form-group text-center"> 
             @Html.CheckBoxFor(model => model.RememberMe, new { @tabindex = "3" }) 
             @Html.LabelFor(model => model.RememberMe) 
            </div> 
            <div class="form-group"> 
             <div class="row"> 
              <div class="col-sm-6 col-sm-offset-3"> 
               <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In"> 
              </div> 
             </div> 
            </div> 
            <div class="form-group"> 
             <div class="row"> 
              <div class="col-lg-12"> 
               <div class="text-center"> 
                <a href="" tabindex="5" class="forgot-password">Forgot Password?</a> 
               </div> 
              </div> 
             </div> 
            </div> 
           </form> 
           @*registration form*@ 
           <form id="register-form" action="" method="post" role="form" style="display: none;"> 
            <div style="padding-left:0px !important" class="col-md-6 col-sm-6"> 
             @*@Html.TextBoxFor(mode => mode.body, new { @style = "border-radius:3px;", @class = "form-control", @id = "VendorDetails", @placeholder = Html.DisplayNameFor(model => model.body), @autocomplete = "off" }) 
             @Html.ValidationMessageFor(model => model.body, null, new { @class = "text-danger" })*@ 
            </div> 
            <div style="padding-right:0px !important" class="col-md-6 col-sm-6"> 
             last name 
            </div> 
            <div class="form-group"> 
             full name 
             @*<input type="email" name="email" id="email" tabindex="1" class="form-control" placeholder="Email Address" value="">*@ 
            </div> 
            <div class="form-group"> 
             email 
             @*<input type="email" name="email" id="email" tabindex="1" class="form-control" placeholder="Email Address" value="">*@ 
            </div> 
            <div class="form-group col-md-6 col-sm-6" style="padding-left:0px !important"> 
             password 
             @*<input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="Password">*@ 
            </div> 
            <div class="form-group col-md-6 col-sm-6" style="padding-right:0px !important"> 
             confirm password 
             @*<input type="password" name="confirm-password" id="confirm-password" tabindex="2" class="form-control" placeholder="Confirm Password">*@ 
            </div> 
            <div class="form-group"> 
             <div class="row"> 
              <div class="col-sm-6 col-sm-offset-3"> 
               <input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now"> 
              </div> 
             </div> 
            </div> 
           </form> 
          </div> 
         </div> 
        </div> 

       </div> 
      </div> 
     </div> 
    </div> 

が、問題は=>のようなその示すいくつかのエラーですenter image description here 絵上記の拳1は私にエラーを与え、私はすべて=>(ともインテリセンス私をxhowingない)=> enter image description here を変更haventはなぜもないことも=>

public class combo 
{ 
    public User User { get; set; } 
    public StudentRegistrationViewModel StudentRegistrationViewModel { get; set; } 
    public StudentFullRegistrationViewModel StudentFullRegistrationViewModel { get; set; } 
} 

thts 1を試してみてくださいthatsのさ働いてください。私を助けてください?

+0

'User'クラスが' IEnumerable'として渡されているので、簡単に '@ model.User.SomeValue'を呼び出すとintelliSenseサポートを受けることができません。したがって、 'IEnumerable'をループして' @foreach(var。x in model.Users){

  • @ x.Name
  • }のようなプロパティにアクセスします。 – ViVi

    答えて

    2

    あなたの必要性がある場合は、あなたのコンボは、会員ユーザがユーザ

    public IEnumerable<User> User { get; set; } 
    

    のIEnumerableをであるので、そのようにそれらを操作するだけで、単一のコントロールを持つことができない複数のユーザー(すべてのユーザー) を持っていた理由ですその1人のユーザーがちょうど

    public User User { get; set; } 
    

    にあなたのクラス定義を変更したり、あなたが必要としている場合は、複数のユーザーを持っているオブジェクトを持っているのforeachを使用して、単一の制御

    にユーザーオブジェクトの各バインド
    関連する問題