asp.net mvc 4 - Access Data from First recordset -


i have linq query returns few rows. has join includes company contact information along detail records (so contact info repeated every row). on view page wish display contact info in header area once , loop through recordset display detail data.

how access contact data in first record without looping?

is there better approach? avoiding stuffing contact fields viewbag variables thought more correct grab them single db query.

btw trick getting code blocks work in here. click code button , paste code directly vs. color formatting works , doesn't.

    <fieldset>     <legend>contact information</legend>     <div class="view-field">         @html.valuefor(m => m.companyname)     </div>     <div class="view-field">         @html.valuefor(m => m.name)     </div>     <div class="view-field">         @html.valuefor(m => m.address1)     </div>     <div class="view-field">         @html.valuefor(m => m.city)     </div>     <div class="view-field">         @html.valuefor(m => m.state)     </div>     <div class="view-field">         @html.valuefor(m => m.zip)     </fieldset>           @foreach (var item in model)         {             <tr>                 <td>                     @html.displayfor(m => item.statusdate)                 </td>                 <td>                     @html.displayfor(m => item.statusname)                 </td>                 <td>                     @html.displayfor(m => item.statususername)                 </td>                 <td>                     @html.displayfor(m => item.statusnote)                 </td>             </tr>         } 

edit working boossss idea, came linq , viewmodel not quite getting me solution works. single recordset subcollection of vendorstatushistory records want, throws error trying display property of viewmodel "does not contain definition... , no extension method... found." tried using boossss viewmodel gave same error. suspect linq query off.

solved updated based on boossss comments.

     public statushistoryview vendorstatushistory(string id)     {         guid pid = guid.parse(id);          statushistoryview query = _db.vendorprofiles              .include("vendorstatushistory")             .include("statuscodes")             .select(s => new statushistoryview                 {                     profileid = s.profileid,                     name = s.name,                     companyname = s.companyname,                     companydba = s.companydba,                     email = s.email,                     phone = s.phone,                     website = s.website,                     address1 = s.address1,                     address2 = s.address2,                     city = s.city,                     state = s.state,                     zip = s.zip,                     vendorstatushistory = s.vendorstatushistories                 }             )             .where(x => x.profileid == pid).singleordefault();;           return query;     }  public class statushistoryview {     public statushistoryview()     {         this.vendorstatushistory = new hashset<vendorstatushistory>();     }     public system.guid profileid { get; set; }     public int statusid { get; set; }      [display(name = "full name")]     public string name { get; set; }      [display(name = "email")]     public string email { get; set; }      [display(name = "phone")]     public string phone { get; set; }      [display(name = "website")]     public string website { get; set; }      [display(name = "company")]     public string companyname { get; set; }      [display(name = "dba")]     public string companydba { get; set; }      [display(name = "address")]     public string address1 { get; set; }      [display(name = "address (extra)")]     public string address2 { get; set; }      [display(name = "city")]     public string city { get; set; }      [display(name = "state")]     public string state { get; set; }      [display(name = "zip code")]     public string zip { get; set; }      public virtual icollection<vendorstatushistory> vendorstatushistory { get; set; } } 

and updated view...

            @model  vendorprofileintranet.models.statushistoryview  <table id="vendortable" width="100%" class="gradea">     <thead>         @foreach (var item in model.vendorstatushistory)         {         <tr>             <th style="width:200px">                 @html.displaynamefor(m => item.datecreated)             </th>             <th>                 @html.displaynamefor(m => item.status)             </th>             <th>                 @html.displaynamefor(m => item.username)             </th>             <th>                 @html.displaynamefor(m => item.notes)             </th>         </tr>             break;             }     </thead>     <tbody>  @foreach (var item in model.vendorstatushistory )         {             <tr>                 <td>                     @html.displayfor(m => item.statusdate)                 </td>                 <td>                     @html.displayfor(m => item.statusname)                 </td>                 <td>                     @html.displayfor(m => item.statususername)                 </td>                 <td>                     @html.displayfor(m => item.statusnote)                 </td>             </tr>          }     </tbody>     </tbody> </table> 

i suggest didn't join company contact information along detail records, instead create viewmodel , pass view's model so:

public class myviewmodel {     public string companyname { get; set; }     public string name { get; set; }     public string address1 { get; set; }     public string city { get; set; }     public string state { get; set; }     public string zip { get; set; }      public list<recorddetail> records { get; set; } }  public class recorddetail {     public datetime statusdate { get; set; }     public string statusname { get; set; }     public string statususername { get; set; }     public string statusnote { get; set; } } 

after can use in view this:

@model myviewmodel <fieldset> <legend>contact information</legend> <div class="view-field">     @html.valuefor(m => m.companyname) </div> <div class="view-field">     @html.valuefor(m => m.name) </div> <div class="view-field">     @html.valuefor(m => m.address1) </div> <div class="view-field">     @html.valuefor(m => m.city) </div> <div class="view-field">     @html.valuefor(m => m.state) </div> <div class="view-field">     @html.valuefor(m => m.zip) </fieldset>       @foreach (var item in model.records)     {         <tr>             <td>                 @html.displayfor(m => item.statusdate)             </td>             <td>                 @html.displayfor(m => item.statusname)             </td>             <td>                 @html.displayfor(m => item.statususername)             </td>             <td>                 @html.displayfor(m => item.statusnote)             </td>         </tr>     } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -