Razor Syntax in MVC with VB - asp.net-mvc

In my view, there is a Model that holds a list of User objects. I save the profile pictures of these users in ~/thumbs folder as <username>.png. Now, I want to display all users with their profile pictures and name. So I've wrote the code as shown below
#Code
For Each item in Model
#<div class="col-md-2">
<img class="img-responsive" src="~/thumbs/#item.username.png" alt="">
</div>
Next
End Code
Here, #item.username.png generates an error because the intellisense of Visual Studio assumes that I want to display the png property of the username of the #item
If I put a space before .png and write the src attribute as ~/thumbs/#item.username .png the error gets resolved, but when I run the page; it doesn't show the images since ultimately the src is converted to thumbs/<username>%20.png
What can I do? Please advise!

use following to avoid compile razor syntax problem
<img class="img-responsive" src="~/thumbs/#(item.username).png" alt="">
refer here quick - reference

As an alternative, as per Daniel's comment, you'll can also pre-format the string, or build the string inline, e.g. with String.Format:
<img class="img-responsive" src="#String.Format("~/thumbs/{0}.png", item.username)" alt="">

Related

Umbraco 7 - How to allow users to add new content fields on-the-fly?

I'm setting up Umbraco 7.7 for the first time and creating the document types and templates for a page that displays the people that work at our organization (includes their names, photos, and bios).
How do I configure it such that the content manager can add another "person"—effectively a cluster of divs with user-editable images and text—without having to manually add another "person" to the template? Using Partial Views seems like part of the solution, but I'm unclear on how to fit it all together.
My template (simplified) currently looks something to the effect of:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = null;
}
<!doctype html>
<html>
<body>
<div class="person-bio">
<img src="/media/person-01-photo.jpg">
<p>#Umbraco.Field("person-01-name")</p>
<p>#Umbraco.Field("person-01-title")</p>
<p>#Umbraco.Field("person-01-bio")</p>
</div>
<div class="person-bio">
<img src="/media/person-02-photo.jpg">
<p>#Umbraco.Field("person-02-name")</p>
<p>#Umbraco.Field("person-02-title")</p>
<p>#Umbraco.Field("person-02-bio")</p>
</div>
<div class="person-bio">
<img src="/media/person-03-photo.jpg">
<p>#Umbraco.Field("person-03-name")</p>
<p>#Umbraco.Field("person-03-title")</p>
<p>#Umbraco.Field("person-03-bio")</p>
</div>
<!-- etc., etc. -->
</body>
</html>
Thank you! Any help would be much appreciated.
You'll probably will want to use the Nested Content control for this. It allows you to add a list of entities (in your case persons) on a document
More documentation about the nested content control can be found here: https://our.umbraco.com/documentation/getting-started/backoffice/Property-Editors/Built-in-Property-Editors/Nested-Content
So from my understanding you don't need a partial view. If it's that simple and you want to output only the div I see you're repeating, then loop it:
#foreach (var person in whateverYourCollectionIs) {
<div class="person-bio">
<img src="/media/person-01-photo.jpg">
<p>#person.GetPropertyValue<string>("pseudoNameFieldAlias")</p>
<p>#person.GetPropertyValue<string>("pseudoTitleFieldAlias")</p>
<p>#person.GetPropertyValue<string>("pseudoBioFieldAlias")</p>
</div>
}
That loop will create the exact same html for each person, but with the appropriate names, Titles, Bio etc. This is not the actual code you get to use but it hopefully leads you to the correct direction.
This is the documentation that will help

How can I get a field?

I'm trying to create a partial view macro that list all items (blog entries). I can read its name, but not the entry field like it's content:
#foreach (var page in CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc"))
{
<div class="article">
<div class="articletitle">#page.Name</div>
<div class="articlepreview">
#Umbraco.Truncate(#page.Field("pageContent"),100)
Read More..
</div>
</div>
<hr/>
}
All pages are defined as a ContentPage (document type) where I've added Page Content (pageContent), type: Richtext editor as a Tab: Content element.
Do I need to cast the page or something to Contentpage?
What Im trying to do Is to give a 100 char long preview of the content on my main page so the users can read a short excerpt before clicking on the item.
Technically they are Properties of a doctype, not Fields.
So I believe this is what you are looking for:
#page.GetProperty("pageContent").Value
And truncate...
#Library.Truncate(#page.GetProperty("pageContent").Value,100)
However in this context you should be able to simply use…
#page.pageContent
So...
#Library.Truncate(#page.pageContent,100)
…should work!
I think this will give you the text you are looking for (I think your problem was just that extra #):
#Umbraco.Truncate(page.Field("pageContent"),100)
If you are running on umbraco 7+, try this:
#Umbraco.Truncate(page.AsDynamic().pageContent, 100)
Hope this helps!

How to loop through properties in a tab and display them using Razor?

I have a Document Type, that has a tab with some properties.
The properties are Upload types, and Simple Editor types.
(Users are supposed to upload images with some image text).
I have not grouped the "Upload" and "Simple Editor" properties, so how do i do this?
Next question,
I want to loop through each group (there should be 3 currently) and display them on my website.
The markup should look like the following:
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
..
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
...
I would like to use Razor for this. Thanks in advance!
For the first part, using the Razor model, you can't. The content object that you get on the front end only contains the properties, the tabs are not included, as they're only really for organising things in the back office.
You CAN get that information using the Umbraco API, but it's pretty database intensive and could potentially be quite slow if you have a lot of properties/tabs.
You'd be better grouping them yourself in your Razor Macro.
for the second part, you can acces the properties of a page via #Model.property. For example:
<div>
<div>#Model.simpleProperty</div>
</div>

Syntax to concatenate a variable with static html

I'm trying to build up a bit of HTML using a mix of razor variables and static content.
Here is where I get stuck: I have a counter variable in my view called section_counter. I want to use that in the ID of the image tag I'm building. However unlike with <% .. %> notation I'm used to, I'm just not able to do what I need.
<img alt="section" id="#section_counter_Section" src=""..... etc
I need the id to look like 3_Section. However if I leave a space between the variable and the word _Section, the value retains that space (3 _Section).
If I use the <text> hint, I get this:
<img alt="section" id="3<text>_Section</text>" src="
In my generated HTML. What am I missing?
Try putting your variable in brackets. (An explicit code nugget)
<img alt="section" id="#(section_counter)_Section" src=""

How to open an image in a new browser tab?

I'm trying to open an image in new brrowser tab like this:
<a href="" target="_blank">
<img height="660px" width="420px"
src="<%= Url.Action("WebPageImage", "WPMManagement", new { id = actualId }) %>"
alt="bild mit webseiten version" />
</a>
I need to show just an image and nothing else (no styles etc.)
What about href? What do I need in it?
Change the href attribute to the URL of your image and you're good to go:
Click here to view the image
EDIT: If you're retrieving images from the database, then you'll need Url.Action rather than Url.Content. Check out this question for a similar discussion about retrieving images from a database.
EDIT #2: Updated the example code to use Url.Action rather than Url.Content

Resources