PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2017 how to use repeated get data from table from one place by using general function take-VBForums
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
    Member
    Join Date
    Apr 2018
    Posts
    60

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,586

    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.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,586

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2018
    Posts
    60

    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
    Member
    Join Date
    Apr 2018
    Posts
    60

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,586

    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.

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2018
    Posts
    60

    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
    Member
    Join Date
    Apr 2018
    Posts
    60

    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
  •  



Featured


Click Here to Expand Forum to Full Width