Example of Remote Data in ASP.NET MVC Kanban Control
This demo presents a task management workflow rendered on a Kanban board using a remote service as the data source. It demonstrates the essential DataUrl property that binds a server action to the board, along with the Columns and KeyField properties.
To Do
In Progress
Done
<template id="kanban-col-todo"><span class="ui-kanban-col-icon ui-kanban-col-todo"></span>To Do</template>
<template id="kanban-col-progress"><span class="ui-kanban-col-icon ui-kanban-col-progress"></span>In Progress</template>
<template id="kanban-col-done"><span class="ui-kanban-col-icon ui-kanban-col-done"></span>Done</template>
@Html.UiKanban(new UiKanbanOptions
{
Id = "remoteDataBoard",
DataSource = new List<object>(),
CardSettings = new UiKanbanCardSettings { TagsField = "Tags", GrabberField = "Priority" },
Columns = new List<UiKanbanColumn>
{
new UiKanbanColumn { Key = "Open", Text = "To Do", Template = "#kanban-col-todo" },
new UiKanbanColumn { Key = "InProgress", Text = "In Progress", Template = "#kanban-col-progress" },
new UiKanbanColumn { Key = "Done", Text = "Done", Template = "#kanban-col-done" }
},
Swimlanes = new List<UiKanbanSwimlane>
{
new UiKanbanSwimlane { Key = "High", Text = "High" },
new UiKanbanSwimlane { Key = "Normal", Text = "Normal" }
},
DataUrl = Url.Action("RemoteCards"),
UpdateUrl = Url.Action("SaveRemoteChange"),
Height = "460px",
AriaLabel = "Release workflow board"
})
<template id="kanban-col-todo"><span class="ui-kanban-col-icon ui-kanban-col-todo"></span>To Do</template>
<template id="kanban-col-progress"><span class="ui-kanban-col-icon ui-kanban-col-progress"></span>In Progress</template>
<template id="kanban-col-done"><span class="ui-kanban-col-icon ui-kanban-col-done"></span>Done</template>
<style>
.ui-kanban-col-icon { align-items: center; border-radius: 3px; color: #fff; display: inline-flex; font-size: 11px; height: 16px; justify-content: center; margin-right: 8px; width: 16px; }
.ui-kanban-col-todo { background: #2563eb; }
.ui-kanban-col-progress { background: #f59e0b; }
.ui-kanban-col-review { background: #9c27b0; }
.ui-kanban-col-done { background: #43a047; }
.ui-kanban-col-todo::before { content: "\2630"; }
.ui-kanban-col-progress::before { content: "\2731"; }
.ui-kanban-col-review::before { content: "\25C9"; }
.ui-kanban-col-done::before { content: "\2714"; }
</style>
<style>
.ui-kanban-card-grabber.sf-low { background: #7ed321; }
.ui-kanban-card-grabber.sf-normal { background: #f5c518; }
.ui-kanban-card-grabber.sf-critical { background: #9c27b0; }
.ui-kanban-card-grabber.sf-breaker { background: #e53935; }
</style>
@Html.UiKanban(new UiKanbanOptions
{
Id = "remoteDataBoard",
DataSource = new List<object>(),
CardSettings = new UiKanbanCardSettings { TagsField = "Tags", GrabberField = "Priority" },
Columns = new List<UiKanbanColumn>
{
new UiKanbanColumn { Key = "Open", Text = "To Do", Template = "#kanban-col-todo" },
new UiKanbanColumn { Key = "InProgress", Text = "In Progress", Template = "#kanban-col-progress" },
new UiKanbanColumn { Key = "Done", Text = "Done", Template = "#kanban-col-done" }
},
Swimlanes = new List<UiKanbanSwimlane>
{
new UiKanbanSwimlane { Key = "High", Text = "High" },
new UiKanbanSwimlane { Key = "Normal", Text = "Normal" }
},
DataUrl = Url.Action("RemoteCards"),
UpdateUrl = Url.Action("SaveRemoteChange"),
Height = "460px",
AriaLabel = "Release workflow board"
})
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class KanbanController : Controller
{
// The page is Views/Kanban/RemoteData.cshtml; it draws what ViewData holds.
public ActionResult RemoteData()
{
return View();
}
/// <summary>Returns the remote-data Kanban sample cards as JSON.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult RemoteCards()
{
return Json(new { result = GetRemoteCards() });
}
/// <summary>Persists a validated CRUD operation from the remote-data Kanban sample.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult SaveRemoteChange(KanbanRemoteChange request)
{
if (request == null || !ApplyRemoteChange(request.Action, request.Card))
{
return new HttpStatusCodeResult(400, "Invalid Kanban card change.");
}
return Json(new { result = GetRemoteCards() });
}
private const string Open = "Open";
private const string InProgress = "InProgress";
private const string Done = "Done";
private static readonly object RemoteSync = new object();
private static readonly List<UiKanbanCard> RemoteStore = new List<UiKanbanCard>
{
new UiKanbanCard { Id = "remote-1", Header = "Load remote work", Content = "This card came from the MVC data action.", Status = Open, Swimlane = "Normal", Order = 1 },
new UiKanbanCard { Id = "remote-2", Header = "Save a board move", Content = "Move this card to persist its new status.", Status = Open, Swimlane = "High", Order = 2 },
new UiKanbanCard { Id = "remote-3", Header = "Verify reload", Content = "Reload the board to read the saved status.", Status = InProgress, Swimlane = "Normal", Order = 3 },
new UiKanbanCard { Id = "remote-4", Header = "Complete the sample", Content = "MVC owns the demo's in-memory source.", Status = Done, Swimlane = "High", Order = 4 }
};
/// <summary>Returns a snapshot of the sample's remote card data.</summary>
private static List<UiKanbanCard> GetRemoteCards()
{
lock (RemoteSync)
{
return RemoteStore.Select(Copy).ToList();
}
}
/// <summary>Applies a validated sample CRUD action to the demo's in-memory data.</summary>
private static bool ApplyRemoteChange(string action, UiKanbanCard card)
{
if (card == null || string.IsNullOrWhiteSpace(card.Id) || card.Id.Length > 100 ||
(card.Status != Open && card.Status != InProgress && card.Status != Done) ||
(card.Swimlane != null && card.Swimlane != "High" && card.Swimlane != "Normal") ||
(card.Header != null && card.Header.Length > 200) || (card.Content != null && card.Content.Length > 2000))
{
return false;
}
lock (RemoteSync)
{
UiKanbanCard existing = RemoteStore.FirstOrDefault(item => item.Id == card.Id);
if (string.Equals(action, "add", StringComparison.OrdinalIgnoreCase))
{
if (existing != null || RemoteStore.Count >= UiKanbanOptions.MaximumCardCount) { return false; }
RemoteStore.Add(Copy(card));
return true;
}
if (string.Equals(action, "delete", StringComparison.OrdinalIgnoreCase))
{
return existing != null && RemoteStore.Remove(existing);
}
if (string.Equals(action, "move", StringComparison.OrdinalIgnoreCase) ||
string.Equals(action, "update", StringComparison.OrdinalIgnoreCase))
{
if (existing == null) { return false; }
existing.Header = card.Header;
existing.Content = card.Content;
existing.Status = card.Status;
existing.Swimlane = card.Swimlane;
existing.Order = card.Order;
return true;
}
return false;
}
}
private static UiKanbanCard Copy(UiKanbanCard card)
{
return new UiKanbanCard
{
Id = card.Id,
Header = card.Header,
Content = card.Content,
Status = card.Status,
Swimlane = card.Swimlane,
Order = card.Order
};
}
/// <summary>A requested mutation of the MVC remote Kanban sample data.</summary>
public class KanbanRemoteChange
{
/// <summary>One of add, update, delete or move.</summary>
public string Action { get; set; }
/// <summary>The card state submitted for the mutation.</summary>
public UiKanbanCard Card { get; set; }
}
}
}
@* 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>
@* Kanbans with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
This sample demonstrates remote data binding in the Kanban component using the DataUrl property. The board asks an action of the server for its cards, and sends a change of a card (a move, an add, an update or a delete) to the action given in the UpdateUrl property.
The actions in this sample keep the cards in memory of the demo server. In your own project they read and write the store of your own data.