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; }
    }
}
2
  • Looks like you trying to associate this with EF. Make sure its a a separate folder (say) ViewModels (not in you models folder).
    – user3559349
    Dec 13, 2015 at 6:20
  • @StephenMuecke, its in a separate project
    – tshoemake
    Dec 13, 2015 at 6:25

6 Answers 6

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.

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

4
  • I can try that. Does the viewmodel look to be ok?
    – tshoemake
    Dec 12, 2015 at 21:21
  • Please see the answer from @Cristian as this bug is related to having a data context chosen when building a view that is not tied to a particular EF context.
    – Dave Lucre
    Sep 4, 2018 at 22:05
  • 1
    Issue is because data context class selected
    – mts396
    Oct 9, 2020 at 3:07
  • If this is a bug, then it is still unfixed in 2021, original post is from 2015. The tip from @mts396 seems to work however
    – DanDan
    May 13, 2021 at 8:27
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.

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.

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.