Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

asp.net mvc - Exclude property of model in insert Entity Framework

I get an error when inserting data into my database. My database columns are:

username, password 

and I added confirmpassword to the model to extend it.

How do I get rid of the confirmpassword to insert the username and password while using the model with confirmpassword property?

My code:

BootstrapTrainingEntities db = new BootstrapTrainingEntities();

var u = new User();

u.username = user.username;
u.password = user.password;
// how to I remove or ignore the confirmPassword property when saving?

db.Users.Add(u);
db.SaveChanges();

Model

[MetadataType(typeof(metadataUser))]
public partial class User
{
    public string confirmPassword { get; set; }
}

public class metadataUser
{
    [Required(ErrorMessage = "Username is required", AllowEmptyStrings = false)]
    [Display(Name ="Username")]
    public string username { get; set; }

    [Required(ErrorMessage ="Password is required", AllowEmptyStrings = false)]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string password { get; set; }

    [Required(ErrorMessage ="Confirmation Password is required", AllowEmptyStrings = false)]
    [Display(Name ="Confiramation Password")]
    [DataType(DataType.Password)]
    [Compare("password",ErrorMessage = "Password does not match")]
    public string confirmPassword { get; set; }
}
question from:https://stackoverflow.com/questions/65908028/entityframework-core-sql-exception-when-using-two-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you need to do is add a viewmodel for the page.First of all you will have model that is generated from entity framework which might look as below.

public class User
{
    public int id{get;set;}

    public string username {get;set;}

    public string password {get;set;}
}

So now create a viewModel for your View .This might look as below.

public class UserViewModel
{
   public int id{get;set;}

   [Required(ErrorMessage = "Username is required", AllowEmptyStrings = false)]
   [Display(Name ="Username")]
   public string username { get; set; }

   [Required(ErrorMessage ="Password is required", AllowEmptyStrings = false)]
   [Display(Name = "Password")]
   [DataType(DataType.Password)]
   public string password { get; set; }

   [Required(ErrorMessage ="Confirmation Password is required", AllowEmptyStrings = false)]
   [Display(Name ="Confiramation Password")]
   [DataType(DataType.Password)]
   [Compare("password",ErrorMessage = "Password does not match")]
   public string confirmPassword { get; set; }
}

and now in your controller action method. you can do as below.

[HttpPost]
public ActionResult AddUser(UserViewModel model)
{
    User user=new User();
    user.username=model.username;
    user.password=model.password;
    db.User.Add(user);
}

Hope it helps !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...