asp.net - MVC4 issue with showing multiple partial views on a single View -
i have been struggling problem no avail. using mvc4 have 3 models wish show on single view via partial views. single view comprised of 3 partial views (3 numerated list tables). can 1 model display in view, can't figure out how display other 2.
here have view controller:
[allowanonymous] public actionresult dashboard() { //database.setinitializer<alertscontext>(null); database.setinitializer<memberuserscontext>(null); //database.setinitializer<clubscontext>(null); //alertscontext db = new alertscontext(); memberuserscontext db = new memberuserscontext(); //clubscontext db = new clubscontext(); //db.memberusers.tolist() return view(db.memberusers.tolist()); }
this have model view (to compile remove 2 aren't under @model ienumerable statement:
@model ienumerable<gmc.models.memberusers> <div class="dashboard_alerts"> @html.partial("dashboardalerts") </div> <div class="dashboard_pending_clubs"> @html.partial("dashboardclubs") </div> <div class="dashboard_verified_members"> @html.partial("dashboardmembers") </div>
in each partial view headers follows:
dashboardclubs
@model ienumerable<gmc.models.clubs>
dashboardmembers
@model ienumerable<gmc.models.memberusers>
dashboardalerts
@model ienumerable<gmc.models.alerts>
now question how pass 3 database contexts dashboard? thoroughly confused , struggling.
lots of ways , make generic, have viewmodel. class create support dashboard view:
public class dashboardviewmodel { public ienumerable<gmc.models.clubs> clubs {get;set;} public ienumerable<gmc.models.memberusers> memberusers {get;set;} public ienumerable<gmc.models.alerts> alerts {get;set;} }
if these different tables in database, should accessed through same context, because context manages connection , other resources. however, without seeing code not go on tangent.
[allowanonymous] public actionresult dashboard() { //populate data our view model(vm) var vm = new dashboardviewmodel{ clubs = db.clubs.tolist(), memberusers = db.memberusers.tolist(), alerts = db.alerts.tolist() }; return view(vm); }
dashboard view:
@model dashboardviewmodel <div class="dashboard_alerts"> @html.partial("dashboardalerts", model.alerts) </div> <div class="dashboard_pending_clubs"> @html.partial("dashboardclubs", model.clubs) </div> <div class="dashboard_verified_members"> @html.partial("dashboardmembers", model.memberusers) </div>
personally, instead of using html.partial use action/renderaction, these allow call action returns partialviewresult, , each action responsible retrieving own data. don't way, if appropriate, action/partial view more reusable way.
this more this:
public actionresult dashboard() { return view(); } public partialviewresult clubs() { .... return partialview(db.clubs.tolist());//this assumes partial view named clubs.cshtml, otherwise you'll need use overload pass view name } public partialviewresult alerts() { .... return partialview(db.alerts.tolist()); }
...
dashboard.cshtml
<div class="dashboard_alerts"> @html.action("alerts") </div> <div class="dashboard_pending_clubs"> @html.action("clubs") </div> <div class="dashboard_verified_members"> @html.action("members") </div>
Comments
Post a Comment