Viewmodel has no key defined
Asked Answered
U

6

8

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; }
    }
}
Unmitigated answered 12/12, 2015 at 20:48 Comment(2)
Looks like you trying to associate this with EF. Make sure its a a separate folder (say) ViewModels (not in you models folder).Hush
@StephenMuecke, its in a separate projectUnmitigated
D
3

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

Doehne answered 12/12, 2015 at 20:55 Comment(4)
I can try that. Does the viewmodel look to be ok?Unmitigated
Please see the answer from @Ladonnalady as this bug is related to having a data context chosen when building a view that is not tied to a particular EF context.Canister
Issue is because data context class selectedDullish
If this is a bug, then it is still unfixed in 2021, original post is from 2015. The tip from @Dullish seems to work howeverPains
L
20

Even if it's an old answered question I thought this might help: When creating a view (i suspect partial view in this case), remove the Data Context Class value. If it is set, even though you are creating a non-entity framework based view, Visual studio thinks you are using Entity Framework so it throws that error.

Ladonnalady answered 31/8, 2017 at 12:53 Comment(0)
D
3

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

Doehne answered 12/12, 2015 at 20:55 Comment(4)
I can try that. Does the viewmodel look to be ok?Unmitigated
Please see the answer from @Ladonnalady as this bug is related to having a data context chosen when building a view that is not tied to a particular EF context.Canister
Issue is because data context class selectedDullish
If this is a bug, then it is still unfixed in 2021, original post is from 2015. The tip from @Dullish seems to work howeverPains
D
2

(If using code first, or step to second step)First, you have to check the model(entity) used by ViewModel(and you use to connect to database directly) in which attribute(column) have set "[Key]" Data Annotation.

Second, you have to contain the primary key attribute(column) in the ViewModel class, and give it "[Key]" Data Annotation in the ViewModel class. (Beacause you have to assign a key for computer to distinguish each row.)

Third, rebuild your whole project, then you can successfully use built-in template to generate View.

Dejesus answered 28/6, 2017 at 1:8 Comment(0)
U
2

While you create a corresponding view for an action method, make sure you leave the Data Context Class blank. Just erase it's default value and you will get rid of this error.

Ulphi answered 27/3, 2019 at 15:3 Comment(0)
N
0

Using the tip from Cristian answer I removed the Data Context Class from the UI when creating the view, but it created a view without the fields from my entities which compound my viewmodel. So, I recreated the view using the class model with the most amount of fields, then changed the @model variable to my viewmodel class, and changed the FQN for each field. Until now I could not get a better solution. It's a pain to have to change every single field.

Norman answered 21/11, 2018 at 19:46 Comment(0)
P
0

Create an empty view by choosing Empty (without model) value of Template field rather than any of the following:

Create
Delete
Details
Edit
List

In case of any of the above selection, you need to specify a value of Data Context Class field. So just go with Empty (without model) option.

does it have to have a primary key?

Not necessarily. It depends on your requirements.

Polycarp answered 28/6, 2019 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.