开发者

Problems with editing using a custom model

开发者 https://www.devze.com 2023-04-03 16:50 出处:网络
I have this data model: public class User { public long UserID { get; set; } [Required(ErrorMessage = \"User name is required.\")]

I have this data model:

public class User
    {
        public long UserID { get; set; }

        [Required(ErrorMessage = "User name is required.")]
        [MaxLength(50, ErrorMessage = "User name cannot be longer than 50 characters.")]
        public string UserName { get; set; }

        [Email]
        [Required(ErrorMessage = "Email is required.")]        
        [MaxLength(100, ErrorMessage = "Email cannot be longer than 100 characters.")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Password is required.")]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]        
        public string Password { get; set; }

        [MaxLength(150, ErrorMessage = "Full name cannot be longer than 150 characters.")]
        public string FullName { get; set; }

        public int UserTypeID { get; set; }

        public virtual UserType UserType { get; set; }
        public virtual ICollection<Page> Pages { get; set; }
    }

and I'm using this model to only edit some fields (password shouldn't be editable):

public class EditUserModel
    {
        public long UserID { get; set; }

        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Email]
        [Required(ErrorMessage = "Email is required.")]
        [MaxLength(100, ErrorMessage = "Email cannot be longer than 100 characters.")]
        public string Email { get; set; }

        [DataType(DataType.Text)]
        [Display(Name = "Full name")]
        [MaxLength(150, ErrorMessage = "Full name cannot be longer than 150 characters.")]
        public string FullName { get; set; }

        public int UserTypeID { get; set; }

        public virtual UserType UserType { get; set; }
    }

but I'm confused on how to pass the EditUserModel to my data context to update it. Sorry if seems elementary, but I'm really stumped.

This is the auto-generated edit action that I modified:

[IsAdministrator]
[HttpPost]
        public ActionResult Edit(EditUserModel user)
        {
            if (Mode开发者_如何学运维lState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserTypeID = new SelectList(db.UserTypes, "UserTypeId", "Name", user.UserTypeID);
            return View(user);
        }

This is the line I'm having trouble with:

db.Entry(user).State = EntityState.Modified;

The reason I created a custom class was to avoid exposing the password from the view.


This can't work because you're trying to save view model.

You could use AutoMapper to rewrite data from view model to your data model. After that you should be able to save changes.

User userModel = Mapper.Map<EditUserModel, User>(user);
userModel = // todo: get password from database
// todo: attach your model to context and save changes

I'm using Entity Framework Code First and that approach works great.

0

精彩评论

暂无评论...
验证码 换一张
取 消