4
Answers

Model are not binging in getting record from db entity framework

Photo of Mark Tabor

Mark Tabor

Aug 06
385
1

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; }
}
C#

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();
        }
    }
}
C#

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();
C#

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<Project.Data_Access.Model.Property"> to "Project_web.ViewModels.Property"

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<property> still the above line give same error can someone share the right code

Answers (4)

3
Photo of Jignesh Kumar
29 39.5k 2.9m Aug 07

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.

public class MyWM
    {
        public MYWM()
        {
            Property = new Property ();
            Student = new Student();
        }
        public List<Property> Properties { get; set; }
        public Student Student { get; set; }
        public string? name { get; set; }
        public string? familyname { get; set; }
        public decimal? productcode { get; set; }
        public Assets AssestD { get; set; }
        
    }
Accepted
3
Photo of Naimish Makwana
130 13.8k 201.5k Aug 07

The error message you’re seeing is because you’re trying to assign a List<Project.Data_Access.Model.Property> to a single Project_web.ViewModels.Property object. These types are not the same, even though they have the same properties.

In your ViewModel, you have a Property object, but what you’re getting from the database is a list of Property objects. You need to change your ViewModel to hold a list of Property objects instead of a single one.

Here’s how you can modify your ViewModel:

public class MyWM
{
    public List<Property> Properties { get; set; }
    public Student Student { get; set; }

    public MyWM()
    {
        Properties = new List<Property>();
        Student = new Student();
    }
}

 

And then in your controller, you can assign the list of properties from the database to your ViewModel:

 

MYWM model = new MYWM();
model.Properties = DbContext.Property.ToList();

 

This way, your ViewModel now holds a list of Property objects, which matches what you’re getting from the database. This should resolve the error you’re seeing. 

Thanks

1
Photo of Mark Tabor
556 2k 486.2k Aug 07

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 ?

1
Photo of Mark Tabor
556 2k 486.2k Aug 07

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<property> still on my index view when i say myviewmodel.myobjectOfentity=Dbcontext.property.TOList() the issue persists.