Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }
    }
}
like image 303
tshoemake Avatar asked Dec 12 '15 20:12

tshoemake


People also ask

How do you define a ViewModel?

ViewModel = Model that is created to serve the view. ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose. View Model is a model class that can hold only those properties that are required for a view.

How do you define a key for an entity?

An entity key must consist of a set of non-nullable, immutable, primitive type properties. The properties that make up an entity key for a given entity type cannot change. You cannot allow more than one possible entity key for a given entity type; surrogate keys are not supported.

Can ViewModel have methods?

At least in my knowledge, as per the design principles, ideally we should not have methods in view model classes and create a separate class to manage interaction between business objects and view models.


2 Answers

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.

like image 100
Cristian Avatar answered Oct 01 '22 16:10

Cristian


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

like image 37
Shyju Avatar answered Oct 01 '22 15:10

Shyju