c# - Sharing data between partials -
i've become little lost trying figure out right way share info between partials. i'll best explain setup:
layouts
_layout.cshtml
- main layout site: top navigation, footer etc.
_sectionlayout.cshtml
- sub-layout of _layout.cshtml , makes main content area of site header , 2 lower columns - left column menu, right loads various page content
partials
_sectionmenu.cshtml
- mark-up left hand column menu, builds menu based on viewmodel expects passed contains enumerable list of menu items
_sectionheader.cshtml
- mark-up section header, builds header menu above
all of above located in shared
folder.
a series of controllers utilize _sectionlayout style have consistent style areas of site related pages. layout specified on these controllers via _viewstart.cshtml
within views
folder each controller. each controller implements actionresult
method both _sectionmenu()
, _sectionheader()
, returning appropriately populated viewmodels section.
the individual pages menu loads have been setup along lines of following:
if (request.isajaxrequest()) homecoming partialview(); else homecoming view();
so can loaded either straight via standard request (which loads header, menu , layout), or partial inserted right hand column of layout. seems work well, until come deciding how load info shared between actions , partials within same controller while trying adhere dry can , cutting downwards on code re-use. problem follows:
within typical controller of type, single total load of page invoke 3 actionresult
methods: _sectionheader()
, _sectionmenu()
, whatever main action was. often, 3 of them need access info same, or similar database queries. example, productcontroller
may have index
action shows overview of product information. header need show name of product, menu show statistics such number of images. of info comes single query returns of product info database, how best able share info between 3 actions? ideas i've had:
viewbag
or viewdata
just open database multiple times , hope orm clever plenty realise doesn't need fetch server multiple times within same request the first 1 seems me rather complicated simple ask. sec feels inexpensive easy way out , seek avoid it. third, i'm not confident plenty i'm using (dapper) know not go server 1 time again same query.
to create things little more complex, of actions controller may not need load main database query. extend illustration above, images alternative in menu of product controller may need run additional query load list of associated images, if beingness requested ajax call, doesn't need load main query header , menu sections.
i'm comfortable logic that, every 1 of actions within controller of type need following, or can dry leveraged here?
public actionresult images(int? id) { if (!checkexists(id)) homecoming httpnotfound(); // if action doesn't need shared data, // load if isn't ajax request, menu/header require if (!request.isajaxrequest()) loadshareddata(); // part of question how handle part // load particular model page var model = getimagesmodel(); if (request.isajaxrequest()) homecoming partialview(model); else homecoming view(model); }
in section 10 similar actions in it, seeing extremely similar code repeated tells me i'm doing wrong. in advance help. if haven't explained well, please inquire , i'll best clarify.
c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-mvc-partialview
No comments:
Post a Comment