Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steve Morgan 1348 posts 4457 karma points c-trib
    Apr 28, 2014 @ 13:27
    Steve Morgan
    0

    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>
    }

    What am I missing here?

     

  • Phillip Turner 98 posts 412 karma points
    Apr 28, 2014 @ 15:18
    Phillip Turner
    0

    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

    if(@Umbraco.Field(fieldName) == "True")
    

    or

    if(@Umbraco.Field(fieldName.ToString()) == "True")
    
  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Apr 28, 2014 @ 19:14
    Jeavon Leopold
    101

    Hi Steve,

    @Umbraco.Field will always return IHtmlString, you need a boolean, so you should try this:

            if(CurrentPage.fieldName){
                <h1>This is a test @i</h1>  
            }
    

    CurrentPage is the dynamic model of the current page.

    Further snippet available here

    Jeavon

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Apr 28, 2014 @ 21:25
    Steve Morgan
    0

     

    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>
            }
        }
    }
  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Apr 28, 2014 @ 22:41
    Jeavon Leopold
    0

    Gotcha, sorry I misread your question! Glad it's working now :-)

Please Sign in or register to post replies

Write your reply to:

Draft