Example of Scrolling in ASP.NET MVC ListView Control
This sample demonstrates the functionalities of infinite scrolling. When user scrolls to the bottom, new data is fetched and added to the existing list through scroll event. This creates an infinite scrolling effect, enhancing the user experience by loading new data dynamically as needed.
${Text} ${Price}${Description}
Food Items
- Hamburger
- Cheeseburger
- Sandwich
- Milkshake
- Muffin
- Pizza
- Onion ring
- Sausage
- Pretzel
- Pancake
@Html.UiListView(new UiListViewOptions { Id = "list-scrolling-down", Height = "500px", Template = "#loadTemplate", Items = (List<UiListItem>)ViewData["foodData"] })
<style>
.control-section { max-width: 700px; margin: 0 auto; padding: 10px 20px; }
.control-section h4 { margin: 14px 0 6px; }
#flat-list, #group-list { display: inline-block; vertical-align: top; width: 48%; min-width: 260px; margin-right: 1%; }
.lv-row { display: grid; grid-template-columns: 44px 1fr; column-gap: 12px; align-items: center; width: 100%; box-sizing: border-box; padding: 6px 4px; }
.lv-contact { grid-template-columns: 44px 1fr; }
.lv-avatar { display: inline-flex; width: 36px; height: 36px; align-items: center; justify-content: center; border-radius: 50%; background: var(--ui-row-hover-bg); font-size: 14px; font-weight: 600; flex: none; }
.food-hamburger { background-image: url('@Url.Content("~/Content/listview/hamburger.jpg")'); }
.food-cheeseburger { background-image: url('@Url.Content("~/Content/listview/cheeseburger.jpg")'); }
.food-sandwich { background-image: url('@Url.Content("~/Content/listview/sandwich.jpg")'); }
.food-milkshake { background-image: url('@Url.Content("~/Content/listview/milkshake.jpg")'); }
.food-muffin { background-image: url('@Url.Content("~/Content/listview/muffin.jpg")'); }
.food-pizza { background-image: url('@Url.Content("~/Content/listview/pizza.jpg")'); }
.food-onionrings { background-image: url('@Url.Content("~/Content/listview/onionrings.jpg")'); }
.food-sausage { background-image: url('@Url.Content("~/Content/listview/sausage.jpg")'); }
.food-pretzel { background-image: url('@Url.Content("~/Content/listview/pretzel.jpg")'); }
.food-pancake { background-image: url('@Url.Content("~/Content/listview/pancake.jpg")'); }
.lv-food { grid-template-columns: 100px 1fr; column-gap: 16px; align-items: start; }
.food-img { grid-row: span 3; width: 100px; height: 100px; border-radius: 4px; background-size: cover; background-position: center; background-color: #bcbcbc; }
.food-name { font-size: 18px; font-weight: 600; }
.food-type { display: inline-block; width: 12px; height: 12px; margin-left: 10px; border: 1px solid; border-radius: 2px; }
.food-type.veg { background: #98e16e; }
.food-type.non-veg { background: #ff4b4b; }
.food-price { font-size: 16px; }
.food-desc { font-size: 14px; color: var(--ui-muted); white-space: normal; }
</style>
<template id="loadTemplate"><div class="lv-row lv-food"><span class="food-img ${ImgClass}"></span><span class="food-name">${Text} <i class="food-type ${TypeClass}"></i></span><span class="food-price">${Price}</span><span class="food-desc">${Description}</span></div></template>
<div class="control-section">
<h4 style="text-align: center; padding-bottom: 10px">Food Items</h4>
@Html.UiListView(new UiListViewOptions { Id = "list-scrolling-down", Height = "500px", Template = "#loadTemplate", Items = (List<UiListItem>)ViewData["foodData"] })
</div>
<script>
var loaded = 1;
var foods = @Html.Raw(Json.Encode(((List<UiListItem>)ViewData["foodData"]).Select(i => new { value = i.Value, text = i.Text, data = i.Data })));
var shown = foods.slice();
// Syncfusion: when the list is scrolled to its end the food items load again, up to three times
document.addEventListener('scroll', function (e) {
var box = e.target;
if (!box.classList || !box.closest || !box.closest('#list-scrolling-down') || loaded >= 3) { return; }
if (box.scrollTop + box.clientHeight < box.scrollHeight - 5) { return; }
loaded++;
shown = shown.concat(foods.map(function (item) { return { value: item.value + '-' + loaded, text: item.text, data: item.data }; }));
window.Lumen.get('list-scrolling-down').setItems(shown);
}, true);
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ListViewController : Controller
{
// The page is Views/ListView/ListScrolling.cshtml; it draws what ViewData holds.
public ActionResult ListScrolling()
{
ViewData["foodData"] = new List<UiListItem>
{
new UiListItem { Value = "list-01", Text = "Hamburger", Data = new { ImgClass = "food-hamburger", TypeClass = "non-veg", Price = "$10", Description = "A patty of ground beef grilled and placed between two halves of a bun with slices of lettuce and mayonnaise" } },
new UiListItem { Value = "list-02", Text = "Cheeseburger", Data = new { ImgClass = "food-cheeseburger", TypeClass = "veg", Price = "$12", Description = "A hamburger with a slice of melted cheese on top of the meat patty, added near the end of the cooking time" } },
new UiListItem { Value = "list-03", Text = "Sandwich", Data = new { ImgClass = "food-sandwich", TypeClass = "veg", Price = "$8", Description = "A combination of vegetables, sliced cheese or meat, placed on or between slices of bread with a layer of ingredients" } },
new UiListItem { Value = "list-04", Text = "Milkshake", Data = new { ImgClass = "food-milkshake", TypeClass = "veg", Price = "$6", Description = "A sweet beverage made by blending milk, ice cream, and flavorings or fruit syrup into a thick, sweet, cold mixture" } },
new UiListItem { Value = "list-05", Text = "Muffin", Data = new { ImgClass = "food-muffin", TypeClass = "veg", Price = "$11", Description = "Muffins are single-serving quick breads, which rise with the help of baking soda or baking powder and eggs instead of yeast" } },
new UiListItem { Value = "list-06", Text = "Pizza", Data = new { ImgClass = "food-pizza", TypeClass = "veg", Price = "$22", Description = "A combination of a flattened disk of bread dough with olive oil, oregano, tomato, mozzarella cheese" } },
new UiListItem { Value = "list-07", Text = "Onion ring", Data = new { ImgClass = "food-onionrings", TypeClass = "veg", Price = "$10", Description = "Consists of a cross-sectional 'ring' of onion dipped in bread crumbs and then deep-fried; variant is made with onion paste." } },
new UiListItem { Value = "list-08", Text = "Sausage", Data = new { ImgClass = "food-sausage", TypeClass = "veg", Price = "$15", Description = "Sausage is a combination of minced/ground meat, a binder, water and seasonings, mild but strongly spiced" } },
new UiListItem { Value = "list-09", Text = "Pretzel", Data = new { ImgClass = "food-pretzel", TypeClass = "veg", Price = "$25", Description = "Made from a rope of dough, the pretzel is briefly boiled and then glazed with egg, salted, and baked" } },
new UiListItem { Value = "list-10", Text = "Pancake", Data = new { ImgClass = "food-pancake", TypeClass = "veg", Price = "$23", Description = "A combination of eggs, milk on a hot surface such as a griddle or frying pan, often frying with oil or butter" } }
};
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The Listview control scroll event allows users to load data using a load on demand concept, where buffered data is fetched only when the scrollbar reaches the end of the scroller. Scroll event provides the necessary details to dynamically add the new data to the ListView.