c# - How to bind dropdownlist in partial view in mvc 4 using model? -


hi using partial view in index.cshtml page. , partial view contains 1 dropdownlist bind dynamically database.

i don't want use viewbag or viewdata binding dropdownlist.

in controller

    // get: reporting/home     public actionresult index()     {         var view = view();          return view;     }      public actionresult _reportingmenu()     {         var dashboardlist = getalldashboards();          return view(dashboardlist);     }       public ienumerable<dashboarddefinition> getalldashboards()     {         return _reortdefinitionservice.getalldashboards();     }  

in index.cshtml

@model ienumerable<portal.domain.reportingmodule.reportdefinition.dashboarddefinition>  @{     viewbag.title = "reporting";     layout = "~/views/shared/_layout.cshtml"; }  <h2>reporting view</h2> @html.actionlink("add new chart", "create", "chart", new { area = "reporting" }, null)  @html.partial("_reportingmenu") 

in partialview

    @model  portal.domain.reportingmodule.reportdefinition.dashboarddefinition      <div class="nav navbar navbar-default navbar-no-margin submenu">          @html.dropdownlist("mobiledropdown1",ienumerable<model.dashboardname>)             <a id="submenuintegratornew" class="btn btn-default submenu-item" href="#">             <i class="fa fa-file-o fa-lg"></i>             <span class="submenu-item-text">new</span>         </a>         <a id="submenuintegratorsave" class="btn btn-default submenu-item" href="#">             <i class="fa fa-floppy-o fa-lg"></i>             <span class="submenu-item-text">save</span>         </a>         <a id="submenuintegratorsaveas" class="btn btn-default submenu-item" href="#">             <i class="fa fa-files-o  fa-lg"></i>             <span class="submenu-item-text">save as</span>         </a>         <a id="submenuintegratoraddchart" class="btn btn-default submenu-item" href="#">             <i class="fa fa-picture-o fa-lg"></i>             <span class="submenu-item-text">add chart</span>         </a>     </div> 

but error of null model.

i don't know how implement in partial view

you should use view model, can contain both list , selected property in it, like:

public class dashboardviewmodel {     public ienumerable<selectlistitem> data { get; set; }     //might not int, example     public int selectedid { get; set; } } 

then populate in controller:

public actionresult _reportingmenu() {     var dashboardlist = getalldashboards();      var dropdowndata = dashboardlist         .select(d => new selectlistitem         {             text = d.name, //need apply correct text field here             value = d.id.tostring() //need apply correct value field here         })         .tolist();      var model = new dashboardviewmodel     {        data = dropdowndata      };      return view(model); } 

then in both views, set model:

 @model dashboardviewmodel 

then in partial can do:

 @html.dropdownlistfor(m => m.selectedid, model.data) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -