Created
October 8, 2011 13:15
-
-
Save kenegozi/1272269 to your computer and use it in GitHub Desktop.
Pass compositioned data to views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adding GeneralView class with IsCurrentUserAdmin bool field, | |
// Derive LayoutView from GeneralView | |
// Derive HomepageView and PostPageView from LayoutView | |
public ActionResult ViewPost(string permalink) { | |
var post = magic.GetPostBy(permalink); | |
var view= new PostView {Post=post}; | |
GetLayoutData(view); | |
view.Related = magic.GetContentRelatedTo(post); | |
Return View(view); | |
} | |
public ActionResult Homepage(){ | |
var posts = magic.GetRecentPosts(); | |
var view= new HomepageView {Posts=posts}; | |
GetLayoutData(view); | |
view.Related = magic.GetContentRelatedToMany(posts); | |
Return View(new HomepageView {Posts = posts}); | |
} | |
private void GetLayoutData(LayoutView layoutView) { | |
// called from each action, or with a Filter, whatever | |
// but if we call this in a filter, how do we get an instance | |
// of the view data instance | |
layoutView.Archive = magic.GetArchive(); | |
layoutView.TagCloud = magic.GetTagCloud(); | |
} | |
private void GetSharedData(GeneralView view) { | |
// called from each action, or with a Filter, whatever | |
// but if we call this in a filter, how do we get an instance | |
// of the view data instance | |
view.IsCurrentUserAdmin = magic.FigureThatOut(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stick data into the ViewData, then within the views, cast into the desired type | |
public ActionResult ViewPost(string permalink) { | |
var post = magic.GetPostBy(permalink); | |
ViewData["PostView"] = new PostView {Post=post}; | |
var layout = GetLayoutData(view); | |
layout.Related = magic.GetContentRelatedTo(post); | |
ViewData["LayoutView"] = layout; | |
Return View(); | |
} | |
public ActionResult Homepage(){ | |
var posts = magic.GetRecentPosts(); | |
ViewData["HomepageView"] = HomepageView {Posts=posts}; | |
var layout = GetLayoutData(view); | |
layout.Related = magic.GetContentRelatedToMany(posts); | |
ViewData["LayoutView"] = layout; | |
Return View(); | |
} | |
private LayoutView GetLayoutData() { | |
var layoutView = new LayoutView(); | |
layoutView.Archive = magic.GetArchive(); | |
layoutView.TagCloud = magic.GetTagCloud(); | |
return layoutView; | |
} | |
private void GetSharedData(GeneralView view) { | |
// called from each action, or with a Filter, whatever | |
// but if we call this in a filter, how do we get an instance | |
// of the view data instance | |
currentContext.ViewData["IsCurrentUserAdmin"] = magic.FigureThatOut(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment