Copied to clipboard

Flag this post as spam?

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


  • Richard Galt 64 posts 230 karma points
    Aug 15, 2014 @ 14:33
    Richard Galt
    0

    Code stopped working after adding property converters

    Hi Jeavon,

    You said to post if I had a problem :)

    I've found that my service alert code has stopped functioning.

    This is the code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
    
        if ( Model.Content.HasProperty("serviceAlert") && Model.Content.HasValue("serviceAlert"))
        {
            @*
                when we have service alerts they are stored as a string of comma seperated ids
                so we split the string, then use the umbraco libraries to get a collection
                of nodes for each valid (and visible) service id
            *@
    
            string serviceAlerts = Model.Content.GetPropertyValue<string>("serviceAlert");
    
            var ids = serviceAlerts.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => int.Parse(x));
    
            var serviceAlertNodes = Umbraco.TypedContent(ids).Where(x => x != null && x.IsVisible());
    
            if ( serviceAlerts.Any() )
            {
                foreach( var alert in serviceAlertNodes.Where(x => x.IsVisible()))
                {
                    string alertType = alert.GetPropertyValue<string>("alertType");
    
                    <div class="container">
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="servicealert alert alert-@alertType">
                                        <h1>@alert.GetPropertyValue("title")</h1>
                                        @Html.Raw(alert.GetPropertyValue<string>("bodyText"))
                                </div>
                            </div>
                        </div>
                    </div>
                }
            }
        }
    }
    

    I get the following error:

    Input string was not in a correct format.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.FormatException: Input string was not in a correct format.

    Source Error:

    Line 14: 
    Line 15:         var ids = serviceAlerts.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries)
    Line 16:             .Select(x => int.Parse(x));
    Line 17: 
    Line 18:         var serviceAlertNodes = Umbraco.TypedContent(ids).Where(x => x != null && x.IsVisible());
    

    Thanks

    Rich

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Aug 15, 2014 @ 15:27
    Jeavon Leopold
    0

    Hi Rich,

    I'm taking a guess that serviceAlert is a multinode tree picker, so now the hard is being done for you already by the converter:

    @inherits UmbracoTemplatePage    
    @{    
        if (Model.Content.HasProperty("serviceAlert") && Model.Content.HasValue("serviceAlert"))
        {  
            var serviceAlerts = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("serviceAlert");
    
            if (serviceAlerts.Any())
            {
                foreach (var alert in serviceAlerts.Where(x => x.IsVisible()))
                {
                    string alertType = alert.GetPropertyValue<string>("alertType");
    
                    <div class="container">
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="servicealert alert alert-@alertType">
                                    <h1>@alert.GetPropertyValue("title")</h1>
                                    @Html.Raw(alert.GetPropertyValue<string>("bodyText"))
                                </div>
                            </div>
                        </div>
                    </div>
                }
            }
        }
    }
    

    Jeavon

  • Richard Galt 64 posts 230 karma points
    Aug 15, 2014 @ 15:46
    Richard Galt
    0

    Thanks Jeavon,

    That's correct its an MNTP.

    I have tried the code and get the following error message :

    Compiler Error Message: CS1061: 'object' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
    
    Source Error:
    
    
    Line 6:             var serviceAlerts = Model.Content.GetPropertyValue("serviceAlert");
    Line 7:      
    Line 8:              if (serviceAlerts.Any())
    Line 9:              {
    Line 10:                 foreach (var alert in serviceAlerts.Where(x => x.IsVisible()))
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Aug 15, 2014 @ 15:48
    Jeavon Leopold
    0

    You need to specify the type on line 6

    var serviceAlerts = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("serviceAlert");
    
  • Richard Galt 64 posts 230 karma points
    Aug 15, 2014 @ 15:54
    Richard Galt
    0

    Ah yes think I get it now :

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
        @{    
            if (Model.Content.HasProperty("serviceAlert") && Model.Content.HasValue("serviceAlert"))
            {  
               var serviceAlerts = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("serviceAlert");
    
                if (serviceAlerts.Any())
                {
                    foreach (var alert in serviceAlerts.Where(x => x.IsVisible()))
                    {
                        string alertType = alert.GetPropertyValue<string>("alertType");
    
    
    
                                        @alert.GetPropertyValue("title")
                                        @Html.Raw(alert.GetPropertyValue("bodyText"))
    
    
    
    
                    }
                }
            }
        }
    

    Thanks again :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Aug 15, 2014 @ 16:13
    Jeavon Leopold
    101

    Great, any usage of the below property editors will need to be updated

  • Richard Galt 64 posts 230 karma points
    Aug 15, 2014 @ 17:23
    Richard Galt
    0

    Thanks again :)

Please Sign in or register to post replies

Write your reply to:

Draft