How to you use an entity frame work list made from a class library and use it in a MVC programs view??

Appdev 0 Reputation points
2023-05-05T20:22:06.49+00:00

Hi All,

I asked a question about passing a list from a class with help I was able to get that to work

https://learn.microsoft.com/en-us/answers/questions/1275797/i-am-trying-to-return-an-entity-framework-list-in?page=1&orderby=helpful&comment=answer-1245226#newest-answer-comment

But now on the MVC side of things... everything has gone out of shape and I can even use the data that I passed in because the view in the mvc program expects something call and ienumrable version of my table but its complaining that it is getting a version of that table from my class. (this kinda of thing makes me want to put a bullet in my head)

So I have read up on things and I thought I could use the trick of making that clone of the table and then reading my results into that clone it was a no go.

I mean seriously what am I missing? of course If I make a list using a class library surely the computer know that I am trying to feed it into a view... why would it try using the version from the class library and not allow me to tell it that I want to take that data from the class library and fit it into the local clone of the database table for the view?

I am so confused now.

When I try this

             IEnumerable DocList = DSDB_Library.GET_DOCUMENTS_BY_DocCode(DocCode);
        public ActionResult Gate(string DocCode)
        {
            DSDBEntities DB_Entities = new DSDBEntities();
            var DSDB_Library = new DSDB_LIBRARY.DBDS_ClassV1();

       
             IEnumerable DocList = DSDB_Library.GET_DOCUMENTS_BY_DocCode(DocCode);
            //var DocList = DB_Entities.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();

            return View(DocList);
            //return View(DB_Entities.DSDB_STORE_TBL.ToList().Find(d =>d.DOC_SOURCE_DISC == DocCode));

       

        }

it compiles but I get this error

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[DSDB_LIBRARY.Models.DSDB_STORE_TBL]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[DSDB_V1.Models.DSDB_STORE_TBL]'.

my source I tried:

        [HttpPost]

        public ActionResult Gate(string DocCode)
        {
            DSDBEntities DB_Entities = new DSDBEntities();
            var DSDB_Library = new DSDB_LIBRARY.DBDS_ClassV1();

            IEnumerable<DSDB_STORE_TBL> Test = new IEnumerable<DSDB_STORE_TBL>();

             IEnumerable DocList = DSDB_Library.GET_DOCUMENTS_BY_DocCode(DocCode);
            //var DocList = DB_Entities.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();

            return View(DocList);
            //return View(DB_Entities.DSDB_STORE_TBL.ToList().Find(d =>d.DOC_SOURCE_DISC == DocCode));

       

        }

I added this thinking I could make a close of my table and then read in the filtered values. But that give this error.

Cannot create an instance of the abstract class or interface 'IEnumerable<DSDB_STORE_TBL>' DSDB-V1 C:\APP_DEV\DSDB_DOCUMENT_STORAGE\DSDB-V1\DSDB-V1\Controllers\SSDMController.cs 102 Active

Of course I can find anything saying why this happened

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,292 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,320 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,241 Reputation points
    2023-05-05T23:08:06.1266667+00:00

    You need to learn C#. You can not create an instance of an abstract class, only a concrete class that implements it. Also a class instance can be cast to any type it inherits or interfaces it implements, but not to another type even if it has the same properties.

    .


  2. Bruce (SqlWork.com) 57,241 Reputation points
    2023-05-09T01:43:08.6966667+00:00

    again. you fail to understand basic c# principals. c# is a strongly typed language. you can not replace one class with another.

    say you have two classes

    public class A
    {
       public string Value {get; set;}
       public string Text {get; set;}
    }
    
    public class B
    {
       public string Value {get; set;}
       public string Text {get; set;}
    }
    
    

    and you have a list of A

    var listA = new List<A>
    {
    	new A { Value = 1, Text = "One"},
    	new A { Value = 2, Text = "Two"},
    };
    
    

    and you want a List<B> to have the "same" values. the only way to do this is via mapping. you can use the linq Select() to map a collection.

    // build List<B> from listA (List<A>)
    var listB = listA.Select(r => new B
    {
    	Value = r.Value,
        Text = r.Text
    }).ToList();
    
    // downcast
    IEnumerable<B> enumerableB = (IEnumerable<B>) listB;
    
    // copy a to b
    var a3 = new A { Value=3, Text = "Three"};
    var b3 = new B
    {
        Value = a3.Value,
        Text = a3.Text
    };
    

    mapping objects with common properties is so common. that there are libraries like automapper.

    so if you want to convert a list of entities to list view models, then you need to map the properties.

    note: typeless languages like javascript, two object or two classes with the same properties are interchangeable (Duck typing).

    0 comments No comments