Field / Page Properties - Using a Variable for the page property name
I feel like I'm doing something stupid here.
I have a true/false property on a content node and in a PartialView I want to check if it's set and do some magic. I have five of these so I wanted to cut the copy paste down by using a dynamic variable to hold the field name.
Here's what I wanted to do:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
string fieldName = "";
for(var i=1; i<5; i++)
{
fieldName = "showSlide" + i.ToString();
if(@Umbraco.Field(fieldName)){
<h1>This is a test @i</h1>
}
}
}
This thows an error:
CS0029: Cannot implicitly convert type 'System.Web.IHtmlString' to 'bool'
A more simple example works (well outputs the value of the true/false field showSlide
@{ string fieldName = "showSlide1"; @Umbraco.Field(fieldName) <h1>This is a test </h1> }
Jeavon - the code segment you've posted isn't right as it would try and return the value of a field called "fieldName" rather than the field that the variable fieldName contains but the snippet you've linked to was the key' @CurrentPage.GetPropertyValue(myVar) !
For anyone trying to do something similar this works - I wanted to loop through four similarly named fields e.g. showSlide1, ..showSlide4 and do something (in this example it outputs a header saying this is a test.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
string fieldName = "showSlide";
for (int i = 1; i < 5; i++){
if(@CurrentPage.GetPropertyValue(string.Concat(fieldName, i.ToString())))
{
<h1>This is a test @i</h1>
}
}
}
Field / Page Properties - Using a Variable for the page property name
I feel like I'm doing something stupid here.
I have a true/false property on a content node and in a PartialView I want to check if it's set and do some magic. I have five of these so I wanted to cut the copy paste down by using a dynamic variable to hold the field name.
Here's what I wanted to do:
This thows an error:
CS0029: Cannot implicitly convert type 'System.Web.IHtmlString' to 'bool'
A more simple example works (well outputs the value of the true/false field showSlide
What am I missing here?
I think the value of "fieldName" is not being returned as a bool, I have seen this before. Trying something like this and see if it works
or
Hi Steve,
@Umbraco.Field will always return IHtmlString, you need a boolean, so you should try this:
CurrentPage is the dynamic model of the current page.
Further snippet available here
Jeavon
Jeavon - the code segment you've posted isn't right as it would try and return the value of a field called "fieldName" rather than the field that the variable fieldName contains but the snippet you've linked to was the key' @CurrentPage.GetPropertyValue(myVar) !
For anyone trying to do something similar this works - I wanted to loop through four similarly named fields e.g. showSlide1, ..showSlide4 and do something (in this example it outputs a header saying this is a test.
Gotcha, sorry I misread your question! Glad it's working now :-)
is working on a reply...