Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
PLEASE HELP ME NOW. I must submit my project tomorrow
For 2 weeks i haven't made a successful save. what is wrong with my codes? i have this error:


The Action results task
C#
<pre>//
        // GET: /Account/Register
        [AllowAnonymous]
        public ActionResult Register()
        {
            ViewBag.RoleName = new SelectList(db.Roles, "RoleName", "RoleName");
            return View();
        }
 
        //
        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model, DateTime? EmploymentEndDate = null)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { 
                    UserName = model.Email, 
                    Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    using (var context = new TimeSheetsEntities())
                    {
                        var u = new Users
                        {
                            UserId = model.UserId,
                            SocialSecurityNumber = model.SocialSecurityNumber,
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            Address1 = model.Address1,
                            ZipCode = model.ZipCode,
                            City = model.City,
                            PhoneNumber1 = model.PhoneNumber1,
                            PhoneNumber2 = model.PhoneNumber2,
                            EmploymentStartDate = model.EmploymentStartDate,
                            EmploymentEndDate = model.EmploymentEndDate,
                            Email = model.Email,
                            Password = model.Password,
                            RoleName = model.RoleName,
                        };
                        context.Users.Add(u);
                        context.SaveChanges();
                    }
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }
 
            // If we got this far, something failed, redisplay form
            return View(model);
        }


The class models:
C#
public class RegisterViewModel
{
    [Required]
    [Range(10000 - 99999, Double.MaxValue, ErrorMessage = "Create 5 digit UserId")]
    [Display(Name = "User Identification")]
    public int UserId { get; set; }
 
    [Required]
    [Display(Name = "Social Security number")]
    public string SocialSecurityNumber { get; set; }
 
    [Required]
    [Display(Name = "Firstname")]
    public string FirstName { get; set; }
 
    [Required]
    [Display(Name = "Lastname")]
    public string LastName { get; set; }
 
    [Required]
    [Display(Name = "Adress Line 1")]
    public string Address1 { get; set; }
 
    [Required]
    [Display(Name = "Adress Line 2")]
    public string Address2 { get; set; }
 
    [Required]
    [Display(Name = "Postal code")]
    public string ZipCode { get; set; }
 
    [Required]
    [Display(Name = "City")]
    public string City { get; set; }
 
    [Required]
    [Display(Name = "Telephone number")]
    public string PhoneNumber1 { get; set; }
    [Required]
    [Display(Name = "Telephon number")]
    public string PhoneNumber2 { get; set; }
 
    [Required]
    [Display(Name = "Date of employment")]
    public DateTime EmploymentStartDate { get; set; }
 
    [Required]
    [Display(Name = "End of Employment")]
    public DateTime? EmploymentEndDate { get; set; }
 
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }
 
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
 
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
 
    [Required]
    [Display(Name = "Role")]
    public string RoleName { get; set; }


Also save to this model class

C#
<pre>public partial class Users
    {
        public Users()
        {
            this.Projects = new HashSet<Projects>();
            this.Stampings = new HashSet<Stampings>();
            this.TimesheetEntries = new HashSet<TimesheetEntries>();
            this.UserProjects = new HashSet<UserProjects>();
        }
    
        public int UserId { get; set; }
        public string SocialSecurityNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }
        public string PhoneNumber1 { get; set; }
        public string PhoneNumber2 { get; set; }
        public System.DateTime EmploymentStartDate { get; set; }
        public Nullable<System.DateTime> EmploymentEndDate { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string RoleName { get; set; }
    
        public virtual ICollection<Projects> Projects { get; set; }
        public virtual Roles Roles { get; set; }
        public virtual ICollection<Stampings> Stampings { get; set; }
        public virtual ICollection<TimesheetEntries> TimesheetEntries { get; set; }
        public virtual ICollection<UserProjects> UserProjects { get; set; }
    }




The error
The model item passed into the dictionary is of type 'WebTime_Sheet.Models.RegisterViewModel', but this dictionary requires a model item of type 'WebTime_Sheet.Models.Users'.
Posted
Comments
Sinisa Hajnal 24-Feb-15 2:14am    
Users is partial class that will be merged into another such partial. You have to change the model you're passing into dictionary.
Richard Deeming 24-Feb-15 7:47am    
REPOST
You have already asked this question in the .NET Framework forum:
http://www.codeproject.com/Messages/5008357/The-model-item-passed-into-the-dictionary-is-of-ty.aspx[^]

DO NOT post the same question more than once on the same site.

1 solution

I'm guessing this line:

C#
public async Task<actionresult> Register(RegisterViewModel model...


Should look like

C#
public async Task <actionresult> Register(Users model...
 
Share this answer
 
v3
Comments
Kriss Legrand 24-Feb-15 3:06am    
Then, the details won't go to registerViewModel, this means you can not logging with the accounts to be created. i tried that... didnt work
Sinisa Hajnal 24-Feb-15 5:11am    
Sorry, find then which line exactly expects your type and adjust the code so it gets it. You may have to write object transforming function that will take one type and return the other.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900