item.GetProperty("bodytext").Value Umbraco - c#

Hello I've had an issue with gathering an object from my Umbraco Blog page specificly cropping it down
#foreach (var item in Model.Content.Children.Where("visible==true"))
{
var BodyTextToCrop = item.GetProperty("bodytext").Value.ToString();
#item.Name<br />
#Umbraco.Truncate(BodyTextToCrop, 2, true)
}

Change your statement to the following:
#foreach (var item in Model.Content.Children.Where(x => x.IsVisible()))
{
var BodyTextToCrop = item.GetProperty("bodytext").Value.ToString();
#item.Name<br />
#Umbraco.Truncate(BodyTextToCrop, 2, true)
}
It looks like your where clause is using the dynamic access of the type and Model.Content could be strongly typed in your case.
Take a look at querying - MVC
Snippet on the page:
//dynamic access
#CurrentPage.Children.Where("Visible")
//strongly typed access
#Model.Content.Children.Where(x => x.IsVisible())

Related

Sort child nodes by date - Razor Umbraco

For the following examples, I'm using a content tree which looks like this:
Content tree
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var home = Model.Content.Descendants().Where(x => x.DocumentTypeAlias == "BlogContainer");
<div class="container">
<ul>
#foreach (var item in home)
{
foreach (var items in item.Children)
{
foreach (var baby in items.Children.OrderBy("date desc"))
{
var date = baby.GetPropertyValue<DateTime>("date");
<li>#items.Name - #baby.Name - #date</li>
}
}
}
</ul>
</div>
}
And the result is Result three I need to collect all items and set order by date
Try and do something like
var allItems = homePage.Descendants("YourItemNodeType")
.Where(item => item.HasValue("date")
&& item.GetPropertyValue<DateTime>
("date") != DateTime.MinValue)
.ToList()
.OrderByDescending(item => item.GetPropertyValue<DateTime>("date"));
This should get you all your items in both category 1 and category 2, i always tend to check if my date is actually set ( you wouldnt need to do that for create date mentioned by #bowserm as that is always there with a value).
Once u got them to List then you can sort them by their set date, i do this on when i list news articles in different parent pages, then you can just have one loop to go through all of them.
First of all, what Umbraco version are you using? It looks like you are using 6+? Is that right? My answer below should work for 6 and 7.
The property you are looking for is called createDate, so you would use something like baby.GetPropertyValue<DateTime>("createDate"). Even better, you should be able to just type baby.CreateDate. Umbraco has exposed all of the default properties that you might want on the IPublishedContent as properties, so you can get at those without having to use GetPropretyValue(...).
Take a look at this Umbraco v6 MVC Razor Cheatsheet. It lists the default properties you can get off of the nodes in Umbraco. The razor syntax for v6 will also be applicable to v7, so this cheat sheet works for both.

Umbraco 7 get media property value

I'm new to Umbraco 7 and MVC. I have added a property called 'teaser' to the media file type. When I upload a media file, the back office interface recognizes the new property and allows me to set its value.
I am, however, unable to figure out how to access that value for use in the interface. Here's the code:
#if (CurrentPage.HasValue("audioFiles")) {
var audioIdList = CurrentPage.audioFiles.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var audioList = Umbraco.TypedMedia(audioIdList);
<ul class="audioFileList"> #{
foreach (var af in audioList) {
<li>
#af.Name<br />
#af.teaser;
</li>
}
}
</ul>
}
When I run this code, it throws an error saying that "'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'teaser'". The Url and the Name are retrieved just fine. It's only the added 'teaser' that's a problem.
Thanks - Jon
try this:
#af.GetPropertyValue("teaser")
You can only use af.teaser if you are using the dynamic "CurrentPage" object. In this case you inherit from a TypedList which gives you strongly typed .Net objects. These do not contain your custom properties.
If you like the dynamics more, you should replace var audioList = Umbraco.TypedMedia(audioIdList); by var audioList = Umbraco.Media(audioIdList);. This will give you a dynamic object.

Umbraco foreach children of a child page

I have news posts within a news page within a homepage on my content structure
Example:
Homepage
- News
-- News Posts
I'm looking to have some of the news feed on my homepage in a foreach statement. In my head it should be as simple as:
#foreach (var homenews in CurrentPage.Children.Children)
{
if (homenews.Name == "News Post")
{
//Do some stuff//
}
}
Obviously that doesn't work so has anybody got any ideas? Thanks
When you're walking the tree you have to remember that a property (or method) like Children or Descendants() will return a collection of objects, so you can't just call Children of a collection. You can only call Children on a single object.
You can find the correct child of the homepage by using something like var newsPage = CurrentPage.Children.Where(x => x.DocumentTypeAlias == "NewsListingPage") and then extract the children of that page.
I ended up getting the news page by its id and then getting it's children from there. The below code worked for me. Thanks guys.
#{
var node = Umbraco.Content(1094);
<p>#node.Id</p> // output is 1094
foreach (var item in node.Children.Where("Visible").Take(3))
{
<p>#item.exampleText</p>
}
}
You need to reference the required node by NodeTypeAlias of it's Document Type.
So assuming the alias of the DocType of your News Posts is “NewsPosts” then...
#foreach (var homenews in #Model.Descendants().Where("NodeTypeAlias == \"NewsPosts\"")).Take(3)
{
<p>#homenews.Name<p>
}
...should return the name of the first 3 news posts.
I had the exact scenario, this is how I got mine working. NodeByID was very useful nad -1 indicates the root
#foreach(var item in Model.NodeById(-1).Children)
{
string itemName = item.Name;
switch(itemName)
{
case "News":
#* News *#
<div id="News">
<h3>#item.Name</h3>
#foreach (var newsPost in item.Children.OrderBy("UpdateDate desc").Take(4).Items)
{
<p>
#newsPost.Title
</p>
}
</div>
}
}

Passing Field Name to Razor #helper in Webmatrix pages C#

This code obtains a listing of unique org names for display within my .cshtml page:
IEnumerable<dynamic> data = db.Query("Select * from provider
where submitter_element = '210300'");
//the 210300 could be any value passed to the query
var items = data.Select(i => new { i.org_name }).Distinct();
foreach(var name in items){
<text>#name.org_name<br></text>
The records in data are each unique themselves, but the data in each field contains the same values i.e. multiple providers have the same org_name.
I want to be able to reuse the data multiple times to create multiple unique lists. I was hoping to pass this to a #helper for display. To that end, I have the following:
#helper ListBoxDistinctDisplay(IEnumerable<dynamic> queryResult)
{
IEnumerable<dynamic> distinctItems = queryResult.Select(i => new { i.org_name }).Distinct();
foreach(var listItem in distinctItems){
<text>#listItem.org_name<br></text>
}
Then in my .cshtml page I do this:
#DisplayHelpers.ListBoxDistinctDisplay(data)
...and BINGO, I get my unique list on my "view" page.
The works perfectly, except as you see I am having to indicate .org_name within the helper.
My question is how can I pass the field name (org_name) into the helper so that my helper can be re-used no matter he field name?
OR...is there a totally different approach all together that I am unaware of?
THANKS!
Since you like to use dynamic, I'll stick with that.
You may want to pass selector:
#helper ListBoxDistinctDisplay(IEnumerable<dynamic> queryResult, Func<dynamic, dynamic> selector)
{
IEnumerable<dynamic> distinctItems = queryResult.Select(x => new {selectedField = selector(x)}).Distinct();
foreach (var listItem in distinctItems)
{
<text>#listItem.selectedField<br/></text>
}
}
Call it:
#DisplayHelpers.ListBoxDistinctDisplay(data, x => x.org_name)

Passing a dictionary to a view Asp.net MVC

I'm pulling data from MongoDB in C# Asp.net MVC2. Here is the code I'm using in the controller.
var mongo = new Mongo();
mongo.Connect();
var db = mongo.GetDatabase("DDL");
var Provinces = db.GetCollection("Provinces");
var documents = Provinces.FindAll().Documents;
ViewData["Document"] = documents;
return View();
Now I'm unsure how to read the data out in the view. The documents dictionary should have some value pairs like:
Name: someName,
Lat: 39.1,
Lon: 77,
note: test not
When I add it in the view like so:
<p><%: ViewData["Document"]%></p>
I get the output:
MongoDB.Driver.Cursor+<>c__Iterator0
Can someone point me in the right direction?
To start off don't use ViewData. Always use strongly typed views.
var mongo = new Mongo();
mongo.Connect();
var db = mongo.GetDatabase("DDL");
var Provinces = db.GetCollection("Provinces");
var documents = Provinces.FindAll().Documents;
return View(documents.ToArray());
Then strongly type the view and iterate over the model:
<% foreach (var item in Model) { %>
<div><%: item %></div>
<% } %>
If you are lucky you might even get IntelliSense in the view that will offer you properties of the model.
ViewData is a container of objects. You need to cast it back to its native type before you can use it. Something like this (assuming that your dictionary is a Dictionary<string,string>:
<p>
Name: <%: ((Dictionary<string, string>)ViewData["Document"])["Name"] %>
...
</p>

Categories