Request specific data in ASP.NET MVC -
Request specific data in ASP.NET MVC -
i want show breadcrumbs on asp.net mvc 4 website. generation of breadcrumbs depends upon context, illustration on forumpage determined differently on blogpage. on 1 hand, there should 1 partialview "breadcrumbs", partialview not per se know context. , don't want 'pollute' viewmodels breadcrumb stuff. thought of 2 possible solutions:
1) create viewmodels derive basemodel contains breadcrumbdata , in views partialview "breadcrumbs" can called given viewmodel model. 2) in actions save breadcrumbdata somewhere (in webforms had httpcontext.items example) partialview "breadcrumbs" can read them.
but not pleased both of these options. there more elegant ways accomplish this?
the partial view breadcrumbs should given context (as model, list of breadcrumbs text , urls).
if assume each view knows breadcrumbs, can pass breadcrumb info view partial view:
if render breadcrumbs in layout page, view should pass breadcrumb info layout page. i'd utilize viewbag
that.
so, here's how i'd (if breadcrumbs not deep , relatively stable):
someview.cshtml
viewbag.breadcrumbs = new list<breadcrmb>() { new breadcrumb(){ title = "home", url = url.action("index", "home") }, new breadcrumb(){ title = topicmodel.forum.title, url = url.action("forum", new { id = topicmodel.forumid }) }, new breadcrumb(){ title = topicmodel.title, url = url.action("viewtopic", new { id = topicmodel.id }) } };
_layout.cshtml
@html.renderpartial( "_breadcrumbs", viewbag.breadcrumbs )
_breadcrumbs.cshtml
@model ienumerable<breadcrumb> @* render breadcrumbs *@
the breadcrumb info generated in more flexible way didn't inquire i'm not going go deeper on part. suffice getting parent list of breadcrumbs view , adding own better.
asp.net-mvc
Comments
Post a Comment