I met with a small issue in my asp.net core mvc application I define viewmodel in my web project and I have separate dataaccesslayer where I have my models
My one class which is present in dataaccess with name Property having the following properties
public partial class Property
{
public string PropertyId { get; set; } = null!;
public string? Description { get; set; }
public decimal? TypeId { get; set; }
public decimal? Worth { get; set; }
public decimal CountryId { get; set; }
public virtual Category? Asset { get; set; }
public virtual Users? User { get; set; }
}
I have one viewmodel In which I have define few more entities with this entity as well because I have to show data from multiple table so I am using viewmodel
My View model code is below
using NuGet.ContentModel;
using Project_DATA_ACCESS.Models;
namespace Project_WEB.ViewModels
{
public class Property
{
public string PropertyId { get; set; } = null!;
public string? Description { get; set; }
public decimal? TypeId { get; set; }
public decimal? Worth { get; set; }
public decimal CountryId { get; set; }
public virtual Category? Asset { get; set; }
public virtual Users? User { get; set; }
}
Public class student
{
Public int studentID{get;set;}
Public string studentName{get;set;}
}
public class MyWM
{
public string? name { get; set; }
public string? familyname { get; set; }
public decimal? productcode { get; set; }
public Assets AssestD { get; set; }
public MYWM()
{
Property = new Property ();
Student=new Student();
}
}
}
You can see my Property class which is generated by entity framework in my model folder of data access layer and the property class which I have in viewmodel both having the same strucuture same number of columns with data type as well but in my controller index action method when I want to give the property the values from db it gives an error
MYWM model = new MYWM ();
MYWM.Property = DbContext.Property.ToList();
My Table is having name Property on this line I am getting this error
DbContext is not null here I am sure I am initializing the db context the error is basically below one
cannot implicitly convert type "System.Collection.Generic.List
Why it is complaining they are not matching even my both classes in viewmodel and in model are maching
even I tried to declare my viewmodel with List
Jignesh KumarPosted Aug 7, 2024, 6:33 AM
Hello Mark,
Above error indicates that you are trying to assign List to single value object property.
You just need to change your viewmodel and need to create two more properties, then you can assign list of property model to your viewmodel property.
Mark TaborPosted Aug 7, 2024, 2:29 PM
dear Jignesh Kumar &
Naimish Makwana thanks for the reply I like to give my viewmodel entity values from database , one more question as my property class is having propertyTypeID which is another table property category i would like to get its text like property type id text as well and show in the list how to do that ?
Mark TaborPosted Aug 7, 2024, 2:22 PM
i already wrote even i made it list still I am unable to assign it to my viewmodel object , a fair question I would like to ask if my Dbcontext class with entity property and my viewmodel is having entity List still on my index view when i say myviewmodel.myobjectOfentity=Dbcontext.property.TOList() the issue persists.
Naimish MakwanaPosted Aug 7, 2024, 5:13 AM
The error message you’re seeing is because you’re trying to assign a
Listto a singleProject_web.ViewModels.Propertyobject. These types are not the same, even though they have the same properties.In your ViewModel, you have a
Propertyobject, but what you’re getting from the database is a list ofPropertyobjects. You need to change your ViewModel to hold a list ofPropertyobjects instead of a single one.Here’s how you can modify your ViewModel:
And then in your controller, you can assign the list of properties from the database to your ViewModel:
This way, your ViewModel now holds a list of
Propertyobjects, which matches what you’re getting from the database. This should resolve the error you’re seeing.Thanks