我有一个用户视图模型供我的用户使用。我想用它来注册。
我不想使用我的数据模型进行注册或登录。
我的 UserViewModel 如下:
public class UserViewModel
{
public int user_id { get; set; } //Primary Key in user table
[Required]
[DisplayName("Email:")]
public string email { get; set; }
[Required]
[DisplayName("First Name:")]
public string f_name { get; set; }
[Required]
[DisplayName("Last Name:")]
public string l_name { get; set; }
[Required]
[DisplayName("Contact Number:")]
public string contact { get; set; }
[Required]
[DisplayName("Gender:")]
public string gender { get; set; }
[Required]
[DisplayName("Blood Type:")]
public string blood_type { get; set; }
[Required]
[DisplayName("Password:")]
[DataType(DataType.Password)]
public string password { get; set; }
[Required]
[DisplayName("Confirm Password:")]
[DataType(DataType.Password)]
[Compare("password")]
public string confirm_password { get; set; }
}
我的注册 ActionMethod 如下:
[HttpPost]
public ActionResult Registration(UserViewModel uvmr)
{
db.users.Add(uvmr);
db.SaveChanges();
return View();
}
我的用户(user.cs)数据模型如下:
public partial class user
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public user()
{
this.appointments = new HashSet<appointment>();
}
public int user_id { get; set; }
public string f_name { get; set; }
public string l_name { get; set; }
public string email { get; set; }
public string contact { get; set; }
public string gender { get; set; }
public string blood_type { get; set; }
public string password { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<appointment> appointments { get; set; }
}
注意:我的用户表中没有确认密码列。
现在错误说无法从 das.Models.ViewModel.UserViewModel 转换为 das.Models.DataModel.user
这个解决方法是什么?