Getting Started with the QRCode
This tutorial explains how to set up a basic Telerik UI for ASP.NET Core QRCode and highlights the major steps in the configuration of the component.
You will initialize a QRCode component that will display a Card with a vacation offer when the QR code is scanned. Next, you will dynamically update the QRCode value through a single button click. Finally, you can run the sample code in Telerik REPL and continue exploring the components.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:
To create a new pre-configured project for the Telerik UI for ASP.NET Core components, you can use a project template.
To manually configure an existing project, follow the First Steps on Windows or First Steps on Mac articles.
1. Prepare the CSHTML File
The first step is to add the required directives at the top of the .cshtml
document:
-
To use the Telerik UI for ASP.NET Core HtmlHelpers:
@using Kendo.Mvc.UI
-
To use the Telerik UI for ASP.NET Core TagHelpers:
@addTagHelper *, Kendo.Mvc
Optionally, you can structure the View content by adding the desired HTML elements like headings, divs, paragraphs, and others.
@using Kendo.Mvc.UI
<div id="qr-wrap" style="text-align: center;">
<h4>Scan the QR code to review a vacation offer.</h4>
</div>
@addTagHelper *, Kendo.Mvc
<div id="qr-wrap" style="text-align: center;">
<h4>Scan the QR code to review a vacation offer.</h4>
</div>
2. Initialize the QRCode
Use the QRCode HtmlHelper or TagHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
attributes of the QRCode element. - The
Value()
options specifies the QRCode value. - Customize the component appearance by using the
Size()
,Color()
, andBorder()
methods.
@using Kendo.Mvc.UI
@(Html.Kendo().QRCode()
.Name("vacationQrCode")
.Value("https://demos.telerik.com/kendo-ui/qrcode/index")
.Size(170)
.Color("#000000")
.Border(border => border.Color("#000000").Width(5))
)
@addTagHelper *, Kendo.Mvc
<kendo-qrcode name="vacationQrCode"
value="https://demos.telerik.com/kendo-ui/qrcode/index"
size="170"
color="#000000">
<border color="#000000" width="5" />
</kendo-qrcode>
3. Configure the Card
The next step is to define the Card component with the vacation information that will be displayed when you scan the QR code.
<div class="card-container">
<div class="k-card k-card-vertical">
<img class="k-card-image" alt="Telerik UI for ASP.NET Core Cards Africa" src="https://demos.telerik.com/aspnet-core/shared/web/cards/south-africa.jpg"/>
<div class="k-card-body">
<h5 class="k-card-title">African Safari</h5>
<p>Africa provides some of the most epic wildlife diversity on the planet. Not many vacations involve sleeping in close quarters with lions, leopards, elephants, buffaloes, rhinos, giraffes and hippos.</p>
</div>
<div class="k-card-actions k-card-actions-vertical k-card-actions-stretched">
<span class="k-button k-button-flat-primary k-button-flat k-button-md k-rounded-md">Book now</span>
<span class="k-button k-button-flat-primary k-button-flat k-button-md k-rounded-md">Check rates</span>
</div>
</div>
</div>
Add the Card declaration in a Telerik REPL sample and insert its URL in the QRCode Value()
method.
@using Kendo.Mvc.UI
@(Html.Kendo().QRCode()
.Name("vacationQrCode")
.Value("https://netcorerepl.telerik.com/mnbmQncO07oE3ier03")
... // Other configuration.
)
@addTagHelper *, Kendo.Mvc
<kendo-qrcode name="vacationQrCode" value="https://netcorerepl.telerik.com/mnbmQncO07oE3ier03">
<!-- Other configuration -->
</kendo-qrcode>
4. (Optional) Reference Existing QRCode Instances
You can reference the QRCode instances that you have created and build on top of their existing configuration:
-
Use the
id
attribute of the component instance to get its reference.<script> $(document).ready(function() { var qrCodeReference = $("#vacationQrCode").data("kendoQRCode"); // qrCodeReference is a reference to the existing QRCode instance of the helper. }); </script>
-
Use the QRCode client-side API to control the behavior of the component. In this example, you will use the
value()
method to dynamically change the value of the QRCode (for example, when a button is clicked).@(Html.Kendo().Button() .Name("updateQRCodeBtn") .Content("Update QRCode") .Events(ev => ev.Click("onBtnClick")) )
@addTagHelper *, Kendo.Mvc <kendo-button name="updateQRCodeBtn" on-click="onBtnClick"> Update QRCode </kendo-button>
<script> function onBtnClick() { var qrCodeReference = $("#vacationQrCode").data("kendoQRCode"); qrCodeReference.value("mailto:clientservice@telerik.com"); } </script>
For more information on referencing specific helper instances, see the Methods and Events article.
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: