Skip to main content
Home  › ... Razor

Data Tutorials

Tutorial HomeData

Example from CSV

CSV files are often used so website-owners can upload data from another system. This example shows us getting the CSV-file through a query and visualizing it here.

Output

@{
  // get the query
  var query = Kit.Data.GetQuery("ProductsFromCSV");

  // get the data from the query
  var products = AsList(query);

  // get the current product or the default - the query always selects one
  var current = AsList(query["Current"]).FirstOrDefault();

  // Get the current url params so we can derive from it for details links
  var urlParams = CmsContext.Page.Parameters;
}
<ul>
  @foreach (var product in products) {
    <!-- this li will have class=selected if it's the current one -->
    <li class='@(product == current ? "selected" : "")'>
      <!-- this creates a link to the current page and product=id -->
      <a href='@Link.To(parameters: urlParams.Set("product", product.EntityId.ToString()))'>
        @product.Name (#@product.Id)
      </a>
      @if (current == product) {
        <br>
        <em>
          @current.Description 
          (<a href="@product.Link" target="_blank">find out more</a>)
        </em>
      }
    </li>
  }
</ul>

Source Code of assets/products.csv.txt

Id;Name;Description;License;Created;Link
1;2sxc;A neat CMS extension for DNN;MIT;01.01.2012;https://2sxc.org/
2;Koi;System to coordinate the CSS-Framework between theme and modules. ;MIT;01.06.2018;https://connect-koi.net/
3;Razor Blade;Helpers for common Razor task;MIT;01.02.2019;https://github.com/DNN-Connect/razor-blade
4;Image Resizer;Automatic, amazing image Resizer;MIT;06.05.2013;https://2sxc.org/learn-extensions/ImageResizer

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
@using System.Linq;
<!-- unimportant stuff, hidden -->


<div @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>
    <h2>Example from CSV</h2>
    <p>
      CSV files are often used so website-owners can upload data from another system. This example shows us getting the CSV-file through a query and visualizing it here. 
      <br>
    </p>

  </div>
</div>

  @{
    // get the query
    var query = Kit.Data.GetQuery("ProductsFromCSV");

    // get the data from the query
    var products = AsList(query);

    // get the current product or the default - the query always selects one
    var current = AsList(query["Current"]).FirstOrDefault();

    // Get the current url params so we can derive from it for details links
    var urlParams = CmsContext.Page.Parameters;
  }
  <ul>
    @foreach (var product in products) {
      <!-- this li will have class=selected if it's the current one -->
      <li class='@(product == current ? "selected" : "")'>
        <!-- this creates a link to the current page and product=id -->
        <a href='@Link.To(parameters: urlParams.Set("product", product.EntityId.ToString()))'>
          @product.Name (#@product.Id)
        </a>
        @if (current == product) {
          <br>
          <em>
            @current.Description 
            (<a href="@product.Link" target="_blank">find out more</a>)
          </em>
        }
      </li>
    }
  </ul>



@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })