ASP.NET MVC DataViewModel

0

Witam,
posiadam dwie klasy User.cs oraz UserViewModel.cs. W pierwszej z nich są zadeklarowane wszystkie właściwości i na jej podstawie tworzona jest tabela w bazie danych. W drugiej klasie przetrzymuję reguły walidacji i używam jej do pracy na formularzach. Mój problem pojawia się w momencie kiedy wywołując metodę Edit z kontrolera chcę pobrać konkretne dane z bazy danych ale w formularzu chcę używać klasy UserViewModel. W momencie użycia klasy UserViewModel do pobrania danych otrzymuję komunikat: "Cannot implicitly convert type 'Repository.Models.User' to 'AmateurSport.Models.UserViewModel' ". Proszę o pomoc w jaki sposób mogę to przekonwertować.

fragment kontrolera:

public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserViewModel user = db.Users.Find(id);
            

            if (user == null)
            {
                return HttpNotFound();
            }
            //ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "Name", user.RoleID);
            //ViewBag.TeamID = new SelectList(db.Teams, "TeamID", "Name", user.TeamID);
            return View(user);
        }

klasa User.cs

public class User
    {
        public User()
        {
            this.Applications = new HashSet<Application>();
        }

        public int UserID { get; set; }
        [StringLength(20)]
        [Display(Name="Imię")]
        public string FirstName { set; get; }
        [StringLength(30)]
        [Display(Name = "Nazwisko")]
        public string LastName { set; get; }
        public DateTime BirthDate { set; get; }
        [StringLength(30)]
        public string Street { set; get; }
        [StringLength(6)]
        public string Code { set; get; }
        [StringLength(30)]
        public string City { set; get; }
        [StringLength(15)]
        public string Phone { set; get; }
        [StringLength(25)]
        [Display(Name = "e-mail")]
        public string Email { set; get; }
        [StringLength(20)]
        public string Password { set; get; }
        [StringLength(1)]
        public string Sex { set; get; }
        [StringLength(50)]
        public string Hobby { set; get; }
        public bool IsCaptain { set; get; }
        public bool IsActive { set; get; }
        public bool ConfirmedAccount { set; get; }
        public int RoleID { set; get; }
        [Display(Name = "Zespół")]
        public int TeamID { set; get; }

        public virtual Team Team { set; get; }
        public virtual Role Role { set; get; }
        public virtual ICollection<Application> Applications { set; get; }

        
    }

klasa UserViewModel.cs

public class UserViewModel
    {
        [Key]
        public int UserId { get; set; }

        [Display(Name = "Imię")]
        [StringLength(20)]
        [Required(ErrorMessage="Pole imię jest wymagane")]
        [RegularExpression(@"[A-Z-ŻŚŁ]{1}[a-zżźńółęąś]+", ErrorMessage="Nieprawidłowa wartość w polu imię")]
        public string FirstName { set; get; }

        [Display(Name = "Nazwisko")]
        [StringLength(30)]
        [Required(ErrorMessage = "Pole nazwisko jest wymagane")]
        [RegularExpression(@"[A-ZŻŹŚŁ]{1}[a-zżźńółęąćś]+", ErrorMessage="Nieprawidłowa wartość w polu nazwisko")]
        public string LastName { set; get; }

        [Display(Name = "Data urodzenia")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Required(ErrorMessage = "Data urodzenia jest wymagana")]
        public DateTime BirthDate { set; get; }

        [Display(Name = "Ulica")]
        [StringLength(30)]
        public string Street { set; get; }

        [Display(Name = "Kod pocztowy")]
        [StringLength(6)]
        [RegularExpression(@"[0-9]{2}[-][0-9]{3}", ErrorMessage="Nieprawidłowy format kodu pocztowego")]
        public string Code { set; get; }

        [Display(Name = "Miasto")]
        [StringLength(30)]
        [RegularExpression(@"[A-ZŻŹŚŁĆ]{1}[a-zżźńółęąćś]+", ErrorMessage = "Nieprawidłowa wartość w polu miasto")]
        public string City { set; get; }

        [Display(Name = "Telefon")]
        [StringLength(11)]
        [RegularExpression(@"[1-9]{1}\d{2}[' ']{1}\d{3}[' ']{1}\d{3}", ErrorMessage = "Nieprawidłowy format numeru telefonu")]
        public string Phone { set; get; }
        
        [Display(Name = "e-mail")]
        [StringLength(35)]
        [Required(ErrorMessage = "Pole e-mail jest wymagane", AllowEmptyStrings = false)]
        [EmailAddress(ErrorMessage="Nieprawidłowa wartość w polu e-mail")]
        public string Email { set; get; }

        [Display(Name = "Hasło")]
        [StringLength(25)]
        [Required(ErrorMessage = "Pole hasło jest wymagane", AllowEmptyStrings = false)]
        public string Password { set; get; }

        [Display(Name = "Powtórz hasło")]
        [StringLength(25)]
        [Required(ErrorMessage = "Pole powtórz hasło jest wymagane")]
        [Compare("Password", ErrorMessage = "Hasła nie są takie same")]
        public string ComparePassword { get; set; }

        [Display(Name = "Płeć")]
        [StringLength(1)]
        [Required(ErrorMessage = "Pole płeć nie może być puste")]
        public string Sex { set; get; }

        [DataType(DataType.MultilineText)]
        [Display(Name = "Hobby")]
        public string Hobby { set; get; }

        public bool IsCaptain { set; get; }
        public bool IsActive { set; get; }
        public bool ConfirmedAccount { set; get; }

        public int RoleID { set; get; }
        [Display(Name = "Zespół")]
        public int TeamID { set; get; }
    }
2

Możesz użyć metody Select - jaką dostarcza Linq i utworzyć nowy obiekt ViewModel. Możesz również skorzystać z biblioteki AutoMapper.

http://www.altcontroldelete.pl/artykuly/biblioteki-warte-poznania-w-c-automapper/

1 użytkowników online, w tym zalogowanych: 0, gości: 1