Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pass compositioned data to views
// 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();
}
// 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
You can’t perform that action at this time.