在一个视图中使用多个模型.net Mvc

时间:2017-05-24 11:26:22

标签: asp.net-mvc entity-framework model

是的,在一个视图中有多种方法可以使用多个模型。我从那里选择一个选项=>并创建一个额外的类,并将这些多个类添加到该类=>

合并的class =>

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

我喜欢=&gt;(我的模特)

    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; }
        }
    }

和我的观点=&gt;

    @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>

但问题是它显示了一些错误,例如=&gt; enter image description here 上面的第一张图片给了我错误,这就是为什么我没有改变所有=&gt;(并且也没有xowing me intellisense)=&gt; enter image description here 我也尝试=&gt;

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

那个人也没有工作。请帮助我吗?

1 个答案:

答案 0 :(得分:2)

这就是为什么你的组合成员用户是IEnumerable of Users

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

所以你有多个用户不能只有一个控件来操纵它们(你的所有用户) 因此,如果您需要让单个用户只需将您的班级定义更改为

 public User User { get; set; }

或者如果您需要拥有多个用户对象,请使用foreach并将每个用户对象绑定到单个控件