locked
Single model with different non related tables in Web Api RRS feed

  • Question

  • User1284890338 posted

    Hi all,

    I am new to ASP.Net Web Api. I am facing a scenario for advance search page. This page has multiple drop downs for which data will be supplied by the web api. Data for these dd should come from different tables in the database and these tables are not related to each other.

    Few doubts : 1. Should i create a single model with Dictionary<string, string> type properties to bind to drop downs.

    2. In such case how will i create my EntityTypeConfiguration class? (as data comes from diff tables with no relation).

    3. How will i populate this model from controller.

    4. How about my dbContext class.

    OR i will have to create say different 5 models then a combined one. And then 5 context classes. and in the contoller i will use all these to populate my combined model.

    Thoughts?

    Tuesday, February 23, 2016 6:48 PM

Answers

  • User36583972 posted

    Hi Harry US11,

    From your description, you can define a ViewModeAll class, It contains different tables. When you access the API, the ViewModeAll class will return all the data. Then you can go through this collection bind your data.

    The following code for your reference.

     public class Mode1
        {
            public int  ID { get; set; }
            public string name { get; set; }     
        }
        public class Mode2
        {
            public int ID { get; set; }
            public string name { get; set; }
        }
        public class ViewModeAll
        {
            public int ID { get; set; }
            public List<Mode1> model1s { get; set; }
            public List<Mode2> model2s { get; set; }
        }

    API:

    // GET api/values
            public IHttpActionResult  Get()
            {
                //add the data from different tables in the database .
                ViewModeAll vall = new ViewModeAll() { ID = 1, model1s = new List<Mode1>() { new Mode1() { ID = 1, name = "naem" }, new Mode1() { ID = 1, name = "naem" } }, model2s = new List<Mode2>() { new Mode2() { ID = 1, name = "naem" }, new Mode2() { ID = 1, name = "naem" } } };
                return Ok(vall);
            }
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2016 9:49 AM