locked
Cannot implicitly convert type 'VirtualMarkets.Models._ViewModel.Tbl_Products_ViewModel' to 'VirtualMarkets.Models.Department.Tbl_Products RRS feed

  • Question

  • User-973886032 posted

    hi guys

    I am copying and pasting code from an old project to a new to create a viewmodel class for my table Tbl_Products and Tbl_Products_ViewModel

    the model classes for both classes are virtual exactly the same, but I get the error

    error message

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0029	Cannot implicitly convert type 'VirtualMarkets.Models._ViewModel.Tbl_Products_ViewModel' to 'VirtualMarkets.Models.Department.Tbl_Products'	VirtualMarkets.Web	 
    

    Here is my code

        public async Task<IActionResult> OnGetAsync(int? id)
            {
                if (id == null)
                {
                    return NotFound();
                }
    
                Tbl_Products = await _context.Tbl_Products
                    .Include(t => t.Tbl_Departments_Category)
                    .Include(t => t.Tbl_Products_BulkPurchase)
    
                     .Where(r => r.IsEnabled).Select(u => new Tbl_Products_ViewModel
                     {
                         ProductID = u.ProductID,
                         Product_Name = u.Product_Name,
                         Description_Long = u.Description_Long,
                         Description_Short = u.Description_Short
          
                     })
    
                    .FirstOrDefaultAsync(m => m.ProductID == id);

    Both classes are the same, and even when I remove some properties of the class, I still get the same error

     public class Tbl_Products
        {
            [Key]
            public int ProductID { get; set; }
            [Required]
            public string Product_Name { get; set; }
            public int SupplierID { get; set; }
            public int CategoryID { get; set; }
            [ForeignKey("CategoryID")]
            public Tbl_Departments_Category Tbl_Departments_Category { get; set; }
    
            [ForeignKey("ProductID")]
            public Tbl_Products_BulkPurchase Tbl_Products_BulkPurchase { get; set; }
            public DateTime EntryDate { get; set; }
            public string Product_Code { get; set; }
            public string Description_Short { get; set; }
            public string Description_Long { get; set; }
            [Required]
            public decimal Price_Unit { get; set; }
            public decimal? Price_Advert { get; set; }
            public bool Stock { get; set; }
    
            public string Tag_Description_One { get; set; }
    
            public string Tag_Description_Two { get; set; }
            public bool IsEnabled { get; set; }
        }

    Tuesday, September 15, 2020 9:34 PM

All replies

  • User1120430333 posted
    here --> Tbl_Products = await _context.Tbl_Products
                    .Include(t => t.Tbl_Departments_Category)
                    .Include(t => t.Tbl_Products_BulkPurchase)
    
                     .Where(r => r.IsEnabled).Select(u => new Tbl_Products_ViewModel  <--- here
                     {
                         ProductID = u.ProductID,
                         Product_Name = u.Product_Name,
                         Description_Long = u.Description_Long,
                         Description_Short = u.Description_Short
          
                     })
    
                    .FirstOrDefaultAsync(m => m.ProductID == id);
    Both classes are the same, and even when I remove some properties of the 
    VirtualMarkets.Models.Department 

    The classes are not the same.    They have two different names. And the classes originated in two different namespaces.

    VirtualMarkets.Models._ViewModel.

    tbl.product.viwemodel  can never be tblproducts  the class names and the namespaces the classes are in must be the same for the cast to be successful.

    Tuesday, September 15, 2020 11:25 PM
  • User-973886032 posted

    VirtualMarkets.Models.Department 

    The classes are not the same.    They have two different names. And the classes originated in two different namespaces.

    VirtualMarkets.Models._ViewModel.

    tbl.product.viwemodel  can never be tblproducts  the class names and the namespaces the classes are in must be the same for the cast to be successful.

    I am confused now, as I have used such code in my previous MVC models and it worked. The only difference was that it was set into a variable value and returned to the model class

    See this

    https://forums.asp.net/t/2157815.aspx?Is+it+possible+to+have+multiple+navigation+classes+

    Wednesday, September 16, 2020 4:32 AM
  • User585649674 posted

    As the error explains objects of one type cannot be assigned to different type. unless the assigned variable is a base class. It is oops concept of multi level inheritance.

    class Car{}

    class Bus{}

    Car c = new Bus(); => throws an error , even if all properties of car and bus are same

    class Car : Vehicle {}

    class Bus : Vehicle {}

    Vehicle v = new Car (); => this is possible by law of inheritance, even though the assigned variable is of different type.

    Change to below code. It should work. ".Select" should create objects of same type as the variable it is getting assigned to.

    Tbl_Products = await _context.Tbl_Products
                    .Include(t => t.Tbl_Departments_Category)
                    .Include(t => t.Tbl_Products_BulkPurchase)
    
                     .Where(r => r.IsEnabled).Select(u => new Tbl_Products 
                     {
                         ProductID = u.ProductID,
                         Product_Name = u.Product_Name,
                         Description_Long = u.Description_Long,
                         Description_Short = u.Description_Short
          
                     })
    Wednesday, September 16, 2020 5:14 AM
  • User1120430333 posted

    afrika

    DA924

    VirtualMarkets.Models.Department 

    The classes are not the same.    They have two different names. And the classes originated in two different namespaces.

    VirtualMarkets.Models._ViewModel.

    tbl.product.viwemodel  can never be tblproducts  the class names and the namespaces the classes are in must be the same for the cast to be successful.

    I am confused now, as I have used such code in my previous MVC models and it worked. The only difference was that it was set into a variable value and returned to the model class

    See this

    https://forums.asp.net/t/2157815.aspx?Is+it+possible+to+have+multiple+navigation+classes+

    CS0029	Cannot implicitly convert type 'VirtualMarkets.Models._ViewModel.Tbl_Products_ViewModel' to 'VirtualMarkets.Models.Department.Tbl_Products'

    I can only tell you what the exception means. You cannot make a class/object/type be another class/object/type. tblproductsviewmodel can never be a tblproducts on a Linq  query where the result of the Linq query  based on one class/object/type is to be converted over to another class/object/ type  on the fly by .NET.  A class is a type. This is OO 101 and it can never happen under any circumstances that a type can be converted over on the fly to be another type based on the code you have posted,

    tblproducts = Linq query returning the result of tblproductviewmodel is an error.

    Wednesday, September 16, 2020 5:48 AM
  • User711641945 posted

    Hi afrika,

    Be sure your `Tbl_products` defines like below:

    public Tbl_Products_ViewModel  Tbl_Products{ get; set; }

    Best Regards,

    Rena

    Wednesday, September 16, 2020 8:12 AM