Reputation: 283
yah there are many way to use multiple model in one view. and i pick one option from there => and is create a extra class and add those multiple class into that class =>
the combined class =>
public class combo
{
public IEnumerable<User> User { get; set; }
public IEnumerable<StudentRegistrationViewModel> StudentRegistrationViewModel { get; set; }
public IEnumerable<StudentFullRegistrationViewModel> StudentFullRegistrationViewModel { get; set; }
}
i did like =>(my model)
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; }
}
}
and my view =>
@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>
but problem is its showing some error like => the fist one above picture is giving me error and thats why i havent change all =>(and also not xhowing me intellisense)=> i also try =>
public class combo
{
public User User { get; set; }
public StudentRegistrationViewModel StudentRegistrationViewModel { get; set; }
public StudentFullRegistrationViewModel StudentFullRegistrationViewModel { get; set; }
}
thts one also not working .please help me?
Upvotes: 0
Views: 62
Reputation: 628
this is why your combo has member user is IEnumerable of Users
public IEnumerable<User> User { get; set; }
so you have more than one user which cannot have only single control to manipulate them (all your users) so if your need is to have single user just change your class definition to
public User User { get; set; }
or if you are in need to have multiple user objects so use foreach and bind each of your user object to single control
Upvotes: 2