What I would like to see is a full MVC version. Currently when you say MVC I assume that you only use the Views and do things like this in it:
<ul>
@*Menu*@
@foreach (var menu in Model.Content.Children.Where(c => c.HasProperty("menuTitle") && !c.GetPropertyValue<bool>("hideInMenu"))) {
var selected = Model.Content.Path.Contains(menu.Id.ToString()) ? " class=\"active\"" : "";
<[email protected](selected)>
<a href="@menu.NiceUrl()">@(menu.GetPropertyValue<string>("menuTitle"))</a>
</li>
}
</ul>
But instead it would be nice if that is done in the Controller and passed to a custom model. So you have something like this:
public class ExampleModel
{
public IEnumerable<MenuItem> MenuItems { get; set; }
}
public class MenuItem
{
public int Id { get; set; }
public string Title { get; set; }
public string Url { get; set; } public HtmlString ActiveClass { get; set; }
}
/// <summary>
/// Return the menu items. These are the children of the passed in node which are allowed to be displayed.
/// </summary>
/// <returns></returns>
private IEnumerable<MenuItem> GetMenuItems(IPublishedContent content, string path)
{
return (
from n in content.Children
where n.HasProperty("menuTitle")
&& !n.GetPropertyValue<bool>("hideInMenu")
select new MenuItem()
{
Id = n.Id,
Title = n.GetPropertyValue<string>("menuTitle"),
Url = n.NiceUrl(), ActiveClass = new HtmlString(path.Contains(n.Id.ToString()) ? " class=\"active\"" : "")
}
);
}
return CurrentTemplate(exampleModel);
Than in your controller you return the custom model which has the all the things you need and your view can look like this:
<ul>
@*Menu*@
@foreach (var menu in Model.MenuItems)
{
<[email protected]>
<a href="@menu.Url">@menu.Title</a>
</li>
}
</ul>
Yep! That's pretty much where I want to get it to.
Regarding the custom model...
I want to do all the grunt work using Examine. Do you think it is acceptable to return Examine search Result objects in the custom model? So for displaying a single post, a Result object will be passed to the view. For displaying a list, an IEnumerable<Result> would be passed to the listing view etc.
The reason I say this is because I don't really like double handling data for the sake of data transfer. I'd rather use the Result object in the views than having to create a strongly typed model for each view.
That sounds very reasonable :-). I'm already happy that you're hijacking routes.
It might be good to remember that I might want to add your custom model to my own model. For example if I have a model for the master page in which I'll add uBlogsy. So I'll need 1 model in which everything for my own master and uBlogsy is available. Somehow it also needs to be extendable for people how don't use controllers and do everything in the view. So it's good if your custom model also has an IPublishedContent property of the current page.
Is uBlogsy MVC open source? In that case I'll probably just take all your controllers and add them to my own project so It'll be fully integrated with my own Controllers and hijacked routes.
Would prefer to see strongly typed models used and not just passing in Result objects or PublishedContent objects. Have a mapping class and map everything in one place before passing to the view. I did a site last year that heavily relied on examine, and even with the old Razor non MVC way I still passed in mapped models.
uBlogsy has many panels on the right column. eg. latest comments, archive, latest posts etc. I've ported over the code from uBlogsy 2.1.1.1 but the partials still request their own data. This is obviously what we don't want.
I realized last night that I cant simply get all the data in the hijacking controller because of these 2 scenarios:
1. When a developer removes a partial, the data retrieval is now redundant.
2. When a developer copies the the view to their on their own custom page (eg. using the list posts view on their home page) - where does the data come from?
So... what do you think the best way of passing data into the partials is? And where should I be querying for that data? Main layout?
Is it possible to have a different controller for each partial view?
1. I think it's best to use child actions for that. So you can still do all logic in a controller, but if you remove the Html.Action from your razor view it's not used anymore. More info here: http://our.umbraco.org/documentation/Reference/Mvc/child-actions
2. This is a good one. Like I said I'll probably copy the code from your controller and use it in my own controller, but not everyone will work like that. Perhaps you can also solve that with a child action?
The issue I have now is people who add properties. Those properties wont be strongly typed. However, they will be able to get the value from model.Content.GetProperty("myalias")
What you want and need from uBlogsy MVC?
I'll be porting uBlogsy to MVC and just wanted some feedback about my current plans.
My immediate concern is porting all the code. So I'll be doing that and releasing asap. It will be a straight port with (probably) nothing new.
In the very near future though I am considering the following (sort of order of preference):
What I would like to see is a full MVC version. Currently when you say MVC I assume that you only use the Views and do things like this in it:
But instead it would be nice if that is done in the Controller and passed to a custom model. So you have something like this:
Than in your controller you return the custom model which has the all the things you need and your view can look like this:
As you can see this gives a much better separation of code and cleaner Views. You can read more about it here: http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers
Jeroen
Yep! That's pretty much where I want to get it to.
Regarding the custom model...
I want to do all the grunt work using Examine. Do you think it is acceptable to return Examine search Result objects in the custom model? So for displaying a single post, a Result object will be passed to the view. For displaying a list, an IEnumerable<Result> would be passed to the listing view etc.
The reason I say this is because I don't really like double handling data for the sake of data transfer. I'd rather use the Result object in the views than having to create a strongly typed model for each view.
Does that sound reasonable?
That sounds very reasonable :-). I'm already happy that you're hijacking routes.
It might be good to remember that I might want to add your custom model to my own model. For example if I have a model for the master page in which I'll add uBlogsy. So I'll need 1 model in which everything for my own master and uBlogsy is available. Somehow it also needs to be extendable for people how don't use controllers and do everything in the view. So it's good if your custom model also has an IPublishedContent property of the current page.
Is uBlogsy MVC open source? In that case I'll probably just take all your controllers and add them to my own project so It'll be fully integrated with my own Controllers and hijacked routes.
Jeroen
I'm with Jeroen actually.
Would prefer to see strongly typed models used and not just passing in Result objects or PublishedContent objects. Have a mapping class and map everything in one place before passing to the view. I did a site last year that heavily relied on examine, and even with the old Razor non MVC way I still passed in mapped models.
Implementation question...
uBlogsy has many panels on the right column. eg. latest comments, archive, latest posts etc. I've ported over the code from uBlogsy 2.1.1.1 but the partials still request their own data. This is obviously what we don't want.
I realized last night that I cant simply get all the data in the hijacking controller because of these 2 scenarios:
1. When a developer removes a partial, the data retrieval is now redundant.
2. When a developer copies the the view to their on their own custom page (eg. using the list posts view on their home page) - where does the data come from?
So... what do you think the best way of passing data into the partials is? And where should I be querying for that data? Main layout?
Is it possible to have a different controller for each partial view?
1. I think it's best to use child actions for that. So you can still do all logic in a controller, but if you remove the Html.Action from your razor view it's not used anymore. More info here: http://our.umbraco.org/documentation/Reference/Mvc/child-actions
2. This is a good one. Like I said I'll probably copy the code from your controller and use it in my own controller, but not everyone will work like that. Perhaps you can also solve that with a child action?
Jeroen
To answer my own question about partial view controllers... http://our.umbraco.org/documentation/Reference/Mvc/child-actions
LOL.
Yes. Child action solves the problem.
The issue I have now is people who add properties. Those properties wont be strongly typed. However, they will be able to get the value from model.Content.GetProperty("myalias")
is working on a reply...