I am working on a simple project for umbraco and I am in need of a category section. That's why I started to create pages for categories. In websites I am using ultimate picker to select categories. Until this point everything is just fine I can see the categories that I choose in websites page.
Websites
- XXX
- YYY
- ZZZ
- ...
Categories
- Fashion
- Electronics
- ...
My problem is listing all fashion selected websites under the Fashion Category page. I couldn't find any example for this. I would be glad if you could help me in this matter.
Which version of Umbraco are you using? If you're using a version that supports Razor scripts then you could create one and do something like this:
#inherits umbraco.MacroEngines.DynamicNodeContext
#using umbraco;
#using umbraco.MacroEngines;
#{
var node = Library.NodeById(identifierOfFashionNode);
foreach (var child in node.Children) {
// do something with the children of the fashion category
}
}
Actual implementation may vary depending on your version, this is suitable precisely for 4.7.2.
Related
I have a few different styles/templates on my site. These templates are all used for compositions all over my site. Here's an example of my Document Types:
Layouts (folder)
Style 1
Style 2
Style 3
Products (folder)
ProductsPage
Product
News (folder)
NewsOverview
NewsPage
That's basically what it looks like. All of these have templates except for Product and NewsPage, but they have Style X as compositions. Here is what my content nodes looks like:
Home
Products
Product 1 (Style 2)
Product 2 (Style 3)
Product 3 (Style 2)
Product 4 (Style 1)
News
Article 1 (Style 1)
Article 2 (Style 3)
and so on. You get the point? The Style X document types are all compositions, so I don't have to style the same page over and over again, but I can still create any style of page I want.
Now, on my parent (for example NewsOverview), I would like to access the properties of the children. Inside the NewsOverview template file, I would then have to do this:
#foreach(var article in Model.Content.Children) {
dynamic image = article;
<img src="#image.OverviewImage[0].Url" />
<p>#article.GetPropertyValue("overviewTitle")</p>
}
I cannot access the properties without using strings or casting to dynamic (basically), although my models are set to Dll and I usually can do Model.Content.PROPERTY with intellisense and so on.
How can I achieve this? How can I access the properties without using dynamic? I understand that the children aren't necessarily all the same, but all my styled pages contains the same properties, they're just arranged differently in the template.
There's a method called Children<T>() that gets all the child documents of a certain type and can thus safely be cast in a foreach:
#foreach(Article article in Model.Content.Children<Article>()) {
<img src="#article.OverviewImage.First().Url" />
<p>#article.OverviewTitle</p>
}
You'll probably want to add some logic around article.OverviewImage.First() as if there is no value for OverviewImage it will throw a null reference exception.
Have you added your parent model to the template?:
#inherits UmbracoViewPage<ContentModels.Product>
#using ContentModels = Umbraco.Web.PublishedContentModels;
You probably need to cast the children to the specific type:
#using ContentModels = Umbraco.Web.PublishedContentModels;
#foreach (var article in Model.Content.Children.Select(child => new ContentModels.NewsPage(child)) {
<img src="#article .OverviewImage.First().Url" />
<p>#article.OverviewTitle</p>
}
.Children() just gets children of type IPublishedContent, because the ModelsBuilder has no idea which types of documents can exist under the current page.
I am designing ASP.NET WEB API service with two interfaces. First interface is read-only and second one is admin interface (to be used with MVC app) with full CRUD support. Does anyone know where I can get more information on such setup, any tutorials, walk thought, sample design document?
Also does it worth splitting these into two interfaces or keep them in same? But problem is that in read-only I expose 2-3 properties for every object while for admin there are 10-15?
Similar setup for WCF or design spec will do.
If I understand what your wanting to do, may I suggest rather than having one URL, i.e. \dataStuff\dataView split off into another view, maybe \dataStuff\edit\ which only admin's have access to, can be done like so:
[Authorize(Roles = "Administrators")]
public ActionResult edit()
{
return View();
}
then next to each data element that your viewing add the following to ONLY admin's through use of User.IsInRole
#foreach (var item in Model.dataTable)
{
#*
write a value from the data table here
*#
#Html.ActionLink("edit", "edit").If(User.IsInRole("Administrators"))
<br/>
}
obviously you don't have to display your data like this, I'm just showing that you add to the end of each element of the database an edit ActionLink IF the user is admin.
This allows your admin to view data like a user and also have the added functionality they need. Code re-use is better than a single view which has two states, Admin and non Admin.
Sorry if this isn't the best explanation, fairly new to MVC
Seems like this concept is sometimes called CQRS.
Sample: http://martinfowler.com/bliki/CQRS.html
I've got a list of for example Vehicle's that I want to render in a single page, but have different view characteristics for each vehicle type.
Vehicle
- VehicleId
- VehicleTypeId (Car/Boat/Truck/etc...)
- Description
- ...
I'd like to on my main AllVehiclesPage
#foreach(var vehicle in Model) //where Model is a List<Vehicle> from AllVehicles
{
//Render proper View
//CarView(vehicle)
//BoatView(vehicle)
//TruckView(vehicle)
}
In each view it will render differently depending on the vehicle, IE Wheel rim size applies to cars/trucks not to boats etc...
I've read a number of the other questions on Stackoverflow about view inheritance, but they don't seem to apply.
Is this best to do with partial Views? Should I create Usercontrols?
Use display templates. Replace your entire foreach loop with the following line of code:
#model IEnumerable<Vehicle>
...
#Html.DisplayForModel()
And now comes the interesting part. You define display templates for each type that you want to handle:
~/Views/Shared/DisplayTemplates/Car.cshtml
~/Views/Shared/DisplayTemplates/Boat.cshtml
~/Views/Shared/DisplayTemplates/Truck.cshtml
Each template will be strongly typed to the corresponding concrete type. So here's what will happen on the Html.DisplayForModel() line:
ASP.NET MVC sees that the model is an IEnumerable<Vehicle>
ASP.NET MVC starts enumerating over this collection
For each element of the collection ASP.NET MVC looks at its concrete runtime type.
ASP.NET MVC looks for a corresponding display template for this type defined in the ~/Views/Shared/DisplayTemplates folder and automatically renders it by passing this template the current element as model.
You will notice something interesting in this pattern: I use the word ASP.NET MVC very much instead of the word the .NET developer. That's nifty because all that the developer has to do is follow the standard conventions and then ASP.NET MVC will do the job for him so that this developer doesn't need to write loops, doesn't need to write if conditions and doesn't need to reinvent wheels.
Normally we use DisplayForModel or EditorForModel to display and edit a single Customer object, respectively.
How to display a list of Customers using these templating scheme?
Assuming you have a collection of customers in your view model
public class MyViewModel
{
public IEnumerable<Customer> Customers { get; set; }
}
you could use the following in your view:
#Html.DisplayFor(x => x.Customers)
and then in the editor/display template (~/Views/Home/DisplayTemplates/Customer.cshtml or ~/Views/Shared/DisplayTemplates/Customer.cshtml):
#model AppName.Model.Customer
<div>#Model.Name</div>
The Customer partial will be then rendered for each element of the customers collection of your main model. The important thing is the naming convention: the partial should be situated in a DisplayTemplates subfolder and called the same as the collection type (Customer).
How about following Haack's tutorial ?
As great as this feature is, there is
one template that’s conspicuously
missing. ASP.NET MVC does not include
a template for displaying a list of
objects in a tabular format.
Earlier
today, ScottGu forwarded an email from
Daniel Manes (what?! no blog! ;) with
a question on how to accomplish this.
Daniel had much of it implemented, but
was trying to get over the last
hurdle. With Brad’s help, I was able
to give him a boost over that hurdle.
Let’s walk through the scenario.
I'm just learning ASP.NET MVC and my first project is to create a simple link directory (like DMOZ).
I can easily build a strongly typed view of a list of subcategories for a category.
I can easily build a strongly typed view of a list of all the sites in a particular category.
Now, here's what I'm having trouble wrapping my head around:
If I'm viewing a particular category, how would I, in the same page view, display two models (sets) of data:
Top of the page: All subcategories for the category being viewed.
Bottom of the page: All sites in the category being viewed.
I don't have the faintest idea of how to return both the subcategory list and the site list to a particular view. Is it possible? Is there a clean way to do it? (Feel free to point me to an online tutorial or book chapter).
There are two approaches: You can either store one list in ViewData and have it not be strongly typed in your view or you can create a separate ViewModel class which takes two or more existing models so you can refer to these models as properties of your strongly typed ViewModel class inside the View itself.
The best source of information I found was the sample chapter of the upcoming ASP.NET MVC 1.0 book. The first chapter was written by Scott Guthrie and can be found here: http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf