传递到字典中的模型项的类型为“WebTime_Sheet.Models.RegisterViewModel”,但此字典需要“WebTime_Sheet.Models.Users”类型的模型项。销 [英] 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'. Pin

查看:86
本文介绍了传递到字典中的模型项的类型为“WebTime_Sheet.Models.RegisterViewModel”,但此字典需要“WebTime_Sheet.Models.Users”类型的模型项。销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请立即帮助我。我必须明天提交我的项目
两周我没有成功保存。我的代码出了什么问题?我有这个错误:





行动结果任务

< pre>  //    
// 获取:/帐户/注册
[AllowAnonymous]
public ActionResult注册()
{
ViewBag.RoleName = new SelectList(db.Roles, RoleName RoleName) ;
return View();
}

//
// POST:/ Account / Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async 任务< ActionResult>注册(RegisterViewModel模型,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)
{
使用(< span class =code-keyword> var context = new TimeSheetsEntities())
{
var u = new 用户
{
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 );

// 有关如何启用帐户确认和密码重置的详细信息,请访问http: / gogo = zh-CN使用此链接发送电子邮件
// 字符串代码= await UserManager.GenerateEmailConfirmationTokenAsync(user.Id) ;
// var callbackUrl = Url.Action(ConfirmEmail,Account, new {userId = user.Id,code = code},protocol:Request.Url.Scheme);
// 等待UserManager.SendEmailAsync(user.Id,确认您的帐户,请点击< a href = \确认您的帐户+ ca llbackUrl +\> here< / a>);

return RedirectToAction( 索引 首页);
}
AddErrors(result);
}

// 如果我们到目前为止,有些事情失败,重新显示表格
return 查看(模型);
}





班级型号:

  public   class  RegisterViewModel 
{
[必填]
[范围( 10000 - 99999 Double .MaxValue,ErrorMessage = 创建5位数的UserId)]
[显示(名称= 用户识别)]
public int UserId { get ; set ; }

[必需]
[显示(名称= 社会安全号码 )]
public string SocialSecurityNumber {获得; set ; }

[必需]
[显示(名称= 名字)]
public string FirstName {获得; set ; }

[必需]
[显示(名称= 姓氏)]
public string LastName {获得; set ; }

[必需]
[显示(名称= 地址第1行 )]
public string Address1 {获得; set ; }

[必需]
[显示(名称= 地址第2行 )]
public string Address2 {获得; set ; }

[必需]
[显示(名称= 邮政编码)]
public string ZipCode {获得; set ; }

[必需]
[显示(名称= 城市)]
public string 城市{获得; set ; }

[必需]
[显示(名称= 电话号码)]
public string PhoneNumber1 {获得; set ; }
[必需]
[显示(名称= 电话号码) ]
public string PhoneNumber2 { get ; set ; }

[必填]
[显示(名称= 就业日期 )]
public DateTime EmploymentStartDate { get ; set ; }

[必需]
[显示(名称= 就业结束 )]
public DateTime? EmploymentEndDate { get ; set ; }

[必填]
[EmailAddress]
[显示(名称= 电子邮件)]
public string 电子邮件{获得; set ; }

[必需]
[StringLength( 100 ,ErrorMessage = {0}长度必须至少为{2}个字符。,MinimumLength = 6 ) ]
[DataType(DataType.Password)]
[显示(名称= 密码)]
public string 密码{获得; set ; }

[DataType(DataType.Password)]
[显示(名称= 确认密码)]
[比较( 密码,ErrorMessage = 密码和确认密码不匹配。)]
public string ConfirmPassword { get ; set ; }

[必需]
[显示(名称= 角色)]
public string RoleName {获得; set ; }





还保存到此型号班级



< pre> public  partial   class 用户
{
< span class =code-keyword> 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 {获得; 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 城市{ 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 电子邮件{ get ; set ; }
public string 密码{ get ; set ; }
public string RoleName { get ; set ; }

public virtual ICollection< Projects>项目{获取; set ; }
public virtual 角色角色{ get < /跨度>; set ; }
public virtual ICollection< Stampings>冲压件{获取; set ; }
public virtual ICollection< TimesheetEntries> TimesheetEntries { get ; set ; }
public virtual ICollection< UserProjects> UserProjects { get ; set ; }
}









错误

传入字典的模型项的类型为'WebTime_Sheet.Models.RegisterViewModel',但此字典需要类型为'WebTime_Sheet.Models.Users'的模型项。

解决方案

我猜这一行:



  public   async 任务< actionresult>注册(RegisterViewModel模型...... 





看起来应该是



< pre lang =c#> public async 任务< actionresult>注册(用户型号......


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

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

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

<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'.

解决方案

I'm guessing this line:

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



Should look like

public async Task <actionresult> Register(Users model...


这篇关于传递到字典中的模型项的类型为“WebTime_Sheet.Models.RegisterViewModel”,但此字典需要“WebTime_Sheet.Models.Users”类型的模型项。销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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