Search code examples
c#asp.net-mvcrazorforeachhttpcookie

Creating a CSV string in Razor foreach


I am trying to parse multiple page IDs into a cookie in Umbraco Razor.

I have this piece of code

@{ 
    var siteroot = Model.AncestorOrSelf(1);
    HttpCookie eventCookie = new HttpCookie("eventCookie");
}

@foreach (var child in siteroot.Descendants("Event").OrderBy("Date"))
{
    if (DateTime.Today <= child.Date)
    {
        @RenderPage("~/macroscripts/RenderEventBox.cshtml", child, false)
    }
}

@{
    Response.Cookies.Add(eventCookie);
}

What I want to do is to create a CSV string in the foreach, with page IDs.

The page IDs should then be inserted in a cookie in which I can check for new IDs since the last time the user visited - for a "This is the new pages since your last visit" functionality.

But I am not sure how to do this.

The outcome that I want should look like

2525,4587,4789,4790,5858,5782,7899

which I then can put into a cookie.


Solution

  • Got it solved quite simple with

    @{
      var csv = "";
    }
    
    @foreach (var child in siteroot.Descendants("Event").OrderBy("Date"))
    {
      csv += "," + @child.Id;
    }