Results 1 to 8 of 8

Thread: how to use repeated get data from table from one place by using general function take

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    how to use repeated get data from table from one place by using general function take

    How to use repeated get data from table from one place by using general function take table name or any solution .

    I get data from table reference but there different on table name selected from this table . so that i will use same get data statement on more controller so that to prevent repeated get data i think to put this on one place by general function or any concept prevent repeated but i cannot do that .

    on controller actione of employeecontroller

    Code:
    var result = await _context.ReferenceFiles.Where(r => r.TableName == "Employees").ToListAsync();
                ViewBag.RefList = result;
    on controller actione of Itemcontroller

    Code:
    var result = await _context.ReferenceFiles.Where(r => r.TableName == "Items").ToListAsync();
                ViewBag.RefList = result;
    on view

    Code:
     @foreach (var itemes in ViewBag.RefList)
    
    {
        <thead>
    
                @itemes.FieldName
    
    </thead>
    }
    Table Reference have 3 fields

    code

    FieldName

    TableName

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to use repeated get data from table from one place by using general function

    In my office, we pretty much always create our own base class for our controllers because we pretty much always have common functionality to put there. You can do the same and declare in it a method that takes the TableName` value as an argument and returns the result. You then call that method in each controller class that inherits that base class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to use repeated get data from table from one place by using general function

    Your base class might look something like this:
    csharp Code:
    1. public class ControllerBase : Controller
    2. {
    3.     protected async List<ReferenceFile> GetReferenceFilesAsync(string tableName)
    4.     {
    5.         return await _context.ReferenceFiles.Where(r => r.TableName == tableName).ToListAsync();
    6.     }
    7. }
    Note that that code as is would require '_context' to be declared in the base class too, rather than in each individual controller. If you don't want to do that, you'd have to pass the context in as an argument too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    Re: how to use repeated get data from table from one place by using general function

    Thank you for reply
    can you tell me which base class i will define function

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    Re: how to use repeated get data from table from one place by using general function

    Thank you for reply
    can you tell me which base class i will define function

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to use repeated get data from table from one place by using general function

    By default, all your controllers inherit the Controller class. Instead, you declare your own class that inherits Controller (in my example, I have named that class ControllerBase) and then you make all your controllers inherit that class instead. E.g. instead of this:
    csharp Code:
    1. public class ThingController : Controller
    2. {
    3.     // Your actions here.
    4. }
    you would have this:
    csharp Code:
    1. public class ControllerBase : Controller
    2. {
    3.     // Common functionality here.
    4. }
    and this:
    csharp Code:
    1. public class ThingController : ControllerBase
    2. {
    3.     // Your actions here.
    4. }
    If you don't understand base classes and inheritance, you should do some reading on the subject.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    Re: how to use repeated get data from table from one place by using general function

    thank you for reply
    this is good thing
    but if i need to load automatically with every controller without call it what i do

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    Re: how to use repeated get data from table from one place by using general function

    and context from which place i will define it
    are context also will defined on controller

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width