asp.net mvc - how to create a view with multiple models mvc 4? -


so works in asp.net mvc 4 project , have problem in view, want create view 2 differnt type of model,first view (index) take ienumerable (models.mymodel) second (subscriber details) take models.mymodel, this model code :

public class subscribersmodel {      [required]     [datatype(datatype.text)]     public string name { get; set; }      [required]     [datatype(datatype.text)]     [stringlength(maximumlength: 10, minimumlength = 7)]     public string cin { get; set; }      [required]     [datatype(datatype.date)]     public datetime birthday { get; set; }      [datatype(datatype.text)]     public string birthplace { get; set; }   } 

my controller code :

 public class subscriberscontroller : controller {     private agencydbentities dbcontext = new agencydbentities();     private subscribe sb = new subscribe();      public actionresult index()     {         var subscribers = sb in dbcontext.subscribes select new subscribersmodel {      cin = sb.cin, name = sb.name, birthday = (datetime)sb.birthday, birthplace = sb.birthplace   };          return view(subscribers);     }      public actionresult details(string id,string cin,string name)     {         var subscriber = new subscribersmodel();         ienumerable<subscribe> list = s in dbcontext.subscribes select s;         foreach (var sb in list)         {             if (sb.cin == id)             {                 subscriber.cin = sb.cin;                 subscriber.name = sb.name;                 subscriber.birthday = (datetime)sb.birthday;                 subscriber.birthplace = sb.birthplace;             }         }         return view(subscriber);     }  } 

my index view :

  @model ienumerable<_3sdwebproject.models.subscribersmodel>  @{     viewbag.title = "subscribers"; }     <div class="sidebar">       //here want show details view      </div>  <div class="content" style="width: 700px; margin-left: 250px; height: 545px;margin-  top: -30px;">   <h2>subscribers</h2> <p>      @html.actionlink("create new", "create") </p>   @styles.render("~/content/css")  <script src="@url.content("~/scripts/table.js")" type="text/javascript"></script>  <table class="altrowstable" id="alternatecolor">  <tr>     <th>         cin     </th>     <th>         name     </th>     <th>         birthday     </th>     <th>         birthplace     </th>      <th class="operations">         operations     </th> </tr>   @foreach (var item in model) { <tr>     <td>         @html.displayfor(modelitem => item.cin)     </td>     <td>         @html.displayfor(modelitem => item.name)     </td>     <td>         @html.displayfor(modelitem => item.birthday)     </td>     <td>         @html.displayfor(modelitem => item.birthplace)     </td>     <td>       @html.actionlink("details", "details", new { id = item.cin }, new { @class = "details-logo"})      </td> </tr> }  </table> </div> 

my details view :

@model _3sdwebproject.models.subscribersmodel  <fieldset> <legend>subscribersmodel</legend>  <div class="display-label">      @html.displaynamefor(model => model.name) </div> <div class="display-field">     @html.displayfor(model => model.name) </div> <div class="display-label">      @html.displaynamefor(model => model.birthday) </div> <div class="display-field">     @html.displayfor(model => model.birthday) </div>  <div class="display-label">      @html.displaynamefor(model => model.birthplace) </div> <div class="display-field">     @html.displayfor(model => model.birthplace) </div> </fieldset> 

so please if have idea or solution appreciate: nb:this picture describe want enter image description here

create new, compound view model includes both of these view models.

public class compoundviewmodel {     public ienumerable<subscribersmodel> allsubscribers {get; set;}      public subscribersmodel selectedsubscriber {get; set;} } 

ideally split view partial views , render these 2 part of compound model them using displayfor<> or editorfor<>. way can reuse view 'subscribermodel' elsewhere in application if need it.

your controller code improved using dependency injection framework (e.g. autofac) inject dependencies newing up.

another alternative, given using same model list , details view, handle client-side using javascript, either manually using jquery or 1 of newer frameworks allows client-side model binding knockout.js or angular.js.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -