Search code examples
c#asp.net-mvcentity-frameworkviewmodel

Viewmodel has no key defined


I must preface this with I'm very new to viewmodels. That being said, I want to have a create view with payment and subscription info, like a paytment registration page. I want to update multiple entities in my EF model and I was planning on doing it via a viewmodel. The problem is when I try to create view based on my controller action.. i get this error: enter image description here

My viewmodel is used a standalone class to get/post data to my view... maybe I'm going about this the wrong way.. does it have to have a primary key? Does it need to be in my db and added as an EF entity? How do i fix this? Thanks

Here is viewmodel code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVCProject.DataAccess;
using System.ComponentModel.DataAnnotations;

namespace MVCProject.Models.ViewModel
{
    public class PaymentSetupViewModel
    {
        //Subscription.cs
        [Required(ErrorMessage = "required")]
            public string Frequency { get; set; }
            public DateTime Date { get; set; }

        //PaymentMethod.cs    
        [Required(ErrorMessage = "required")]
            [CreditCard]
            [Display(Name = "Card Number")]
            public string CCNumber { get; set; }

            [Required(ErrorMessage = "required")]
            [Display(Name = "Card Expiration")]
            public DateTime CCExpiration { get; set; }

            [Required(ErrorMessage = "required")]
            [Display(Name = "CVV2")]
            public string CCCVV2 { get; set; }

            [Required(ErrorMessage = "required")]
            [Display(Name = "Bank Name")]
            public string BankName { get; set; }

            [Required(ErrorMessage = "required")]
            [Display(Name = "Account Number")]
            public string BankAccountNumber { get; set; }

            [Required(ErrorMessage = "required")]
            [Display(Name = "Routing Number")]
            public string BankRoutingNumber { get; set; }

            [Required(ErrorMessage = "required")]
            public string ProductName { get; set; }

        //AspNetUser.cs properties -- identity list of logins
        public string UserName { get; set; }

        //PaymentSubscriptionViewModels.cs properties    
        public int SelectedValue { get; set; }
    }
}

Solution

  • Does it have to have a primary key?

    No. View model is a simple POCO class. Unless you want to do some custom validation in your UI/Validation/Business layer, you do not need to decorate any property with [Key] attribute.

    Does it need to be in my db and added as an EF entity?

    No. View model's purpose it to communicate data between your view and the action methods. You will read data from one view model object and save that in 2 or more tables as needed. view models should be lean and flat because it is for the specific view.

    The error you are seeing is may be a bug in visual studio. Why don't you manually create an action method, a view (don't select the model in the wizard) and update the view to use your view model as the model