asp.net mvc 3 - MVC3 referencing foreign key during display/edit and ADO.NET Entity Data Model? -


i'm new mvc programming , developing application. have following problem i'm short of ideas how solve (though i've read similiar topics here). want in view display value foreign key column (instead of fk value, take column value related table). i'm not sure how achieve (through model linq statement, create view model, in controller, view?). knowledge in asp.net short appreciate more detailed answer how make it. thank in advance ! check bellow case.

i have following tables in database:

create table [dbo].[tbl_manager]( [manager_id] [int] identity(1,1) not null, [first_name] [varchar](50) null, [last_name] [varchar](50) not null, [team_id] [int] not null,  create table [dbo].[tbl_team]( [team_id] [int] identity(1,1) not null, [name] [varchar](50) not null, 

i use ado.net entitiy data model create class tbl_manager. in model class have following method (maybe should return ilist):

public partial class tbl_manager {     public static iqueryable<tbl_manager> getmanagersquery()     {         fbmsentities db = singletondb.getdb();         var querymanager = manager in db.tbl_manager                            orderby manager.manager_id                            select manager;         return querymanager;     } } 

this code controller (using nuget pagedlist):

    public actionresult index(int? page)     {         var managers = tbl_manager.getmanagersquery();         var pagenumber = page ?? 1;         var onepageofproducts = managers.topagedlist(pagenumber, @fbms.common.constants.max_per_page);         viewbag.onepageofproducts = onepageofproducts;          return view(managers);     } 

and part in view visualize data:

@foreach (var item in viewbag.onepageofproducts) { <tr>     <td>         @item.team_id     </td>     <td>         @item.first_name     </td>     <td>         @item.last_name     </td>     <td>         @item.manager_status     </td>     <td>         @html.actionlink("edit", "edit", new { id=item.manager_id }) |         @html.actionlink("details", "details", new { id=item.manager_id }) |         @html.actionlink("delete", "delete", new { id=item.manager_id })     </td> </tr> } 

i want instead of team_id display value of tbl_team.name.

just you're assigning viewbag.onepageofproducts, can assign dictionary of teams viewbag.teams (so each dictionary item's key team_id , value name) , in view like

...  <td>     @viewbag.teams[item.team_id] </td>  .... 

which name of team id , render it.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -