Copied to clipboard

Flag this post as spam?

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


  • krzakivan 26 posts 92 karma points
    Feb 10, 2019 @ 14:09
    krzakivan
    0

    CS0029: Cannot implicitly convert type 'int' to 'System.Web.IHtmlString'

    Hello guys ,

    I do get error message when I try to do If equal then ..
    CS0029: Cannot implicitly convert type 'int' to 'System.Web.IHtmlString'

    Code:

    var test = @Umbraco.Field("test");
                @if (test = 1 ){
                .....
                } else { .... } 
    

    In backoffice is used Umbraco.Decimal editor for that test property.

    Could someone help to make it work ?

    Thank you

  • Matthew Wise 191 posts 799 karma points c-trib
    Feb 10, 2019 @ 16:18
    Matthew Wise
    0

    Hi,

    Umbraco.Field always returns IHtmlString.

    If you want a typed version you will want either Model.Content.GetPropertyValue

    Or

    Model.GetPropertyValue

    You will want the first one if you view inherits UmbracoTemplatePage and the Second is for UmbracoViewPage

    Matt

  • krzakivan 26 posts 92 karma points
    Feb 10, 2019 @ 16:48
    krzakivan
    0

    Used first option,

    now Im getting Cannot implicitly convert type 'object' to 'bool'. An explicit conversion exists (are you missing a cast?)

  • Matthew Wise 191 posts 799 karma points c-trib
    Feb 10, 2019 @ 16:49
    Matthew Wise
    0

    To do a compare in not an assignment you need to use == in the if

  • krzakivan 26 posts 92 karma points
    Feb 10, 2019 @ 16:53
    krzakivan
    0

    Hi again,

    Tried , but then im left with CS0019: Operator '==' cannot be applied to operands of type 'object' and 'int'

  • Matthew Wise 191 posts 799 karma points c-trib
    Feb 10, 2019 @ 16:55
    Matthew Wise
    0

    Could you show me the code as it is now :)

  • Matthew Wise 191 posts 799 karma points c-trib
    Feb 10, 2019 @ 16:58
    Matthew Wise
    0

    @{ var test = Model.Content.GetPropertyValue

    Am on my phone so can't indent but that should work

  • krzakivan 26 posts 92 karma points
    Feb 10, 2019 @ 17:06
    krzakivan
    0
    var test = Model.Content.GetPropertyValue("test");
    
     @if (test == 1 ){
    
  • Matthew Wise 191 posts 799 karma points c-trib
    Feb 10, 2019 @ 17:08
    Matthew Wise
    0

    GetPropertyValue("test"); Should be

    GetPropertyValue<int>("test");
    
  • krzakivan 26 posts 92 karma points
    Feb 10, 2019 @ 17:23
    krzakivan
    0

    Thank you , It is working now , I just did not know how to specify it as a INT Have a great day Thanks

Please Sign in or register to post replies

Write your reply to:

Draft