|
-
Dec 29th, 2018, 08:13 PM
#1
Thread Starter
Lively Member
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
-
Dec 29th, 2018, 09:15 PM
#2
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.
-
Dec 29th, 2018, 09:39 PM
#3
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:
public class ControllerBase : Controller { protected async List<ReferenceFile> GetReferenceFilesAsync(string tableName) { return await _context.ReferenceFiles.Where(r => r.TableName == tableName).ToListAsync(); } }
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.
-
Dec 30th, 2018, 02:49 AM
#4
Thread Starter
Lively Member
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
-
Dec 30th, 2018, 02:49 AM
#5
Thread Starter
Lively Member
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
-
Dec 30th, 2018, 02:58 AM
#6
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:
public class ThingController : Controller { // Your actions here. }
you would have this:
csharp Code:
public class ControllerBase : Controller { // Common functionality here. }
and this:
csharp Code:
public class ThingController : ControllerBase { // Your actions here. }
If you don't understand base classes and inheritance, you should do some reading on the subject.
-
Dec 30th, 2018, 05:29 AM
#7
Thread Starter
Lively Member
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
-
Dec 30th, 2018, 05:31 AM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|