...
To help with this, below is shown the Razor template for the standard String type, and some commentary to indicate how it works.
Code Block | ||
---|---|---|
| ||
@using Lynicon.Utility |
...
@if (ViewData.ModelMetadata.AdditionalValues.ContainsKey("StringLength"))
{
|
...
ViewData["classes"] = (ViewData["classes"] ?? "") + " char-count"; } <span class="text-display" |
...
> @(Model == null || ViewData.TemplateInfo.FormattedModelValue == "" ? "---" : ViewData.TemplateInfo.FormattedModelValue) </span> @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, ViewData.ModelMetadata.IsReadOnly ? (object)new { @class = "text-box single-line " + ViewData["classes"], @readonly = true } : (object)new { @class = "text-box single-line " + ViewData["classes"] }) @if (ViewData.ModelMetadata.AdditionalValues.ContainsKey("StringLength")) { |
...
<div class="character-count" |
...
> @(Model == null || Model.ToString() == "" ? 0 : Model.ToString().Length) </div> |
...
@Html.RegisterScript("lyn-char-count", @"javascript: |
...
$(document).ready(function () { |
...
$('#edit').on('keyup', '.text-box.char-count', function () { |
...
$(this).next('.character-count').text(($(this).val() || '').length); |
...
}); |
...
});", new List<string> { "jquery" }) } |
This script does not declare a model type: this then means the model is any object. This is necessary for the String template as it can be used to render unknown types, in which case they are converted to a string using ToString().
The template creates the display of the string as a span element, together with an input element which starts off hidden. A part of the global editor script file deals with this functionality, so you can't see it in the template file. However, the template shows the technique of supplying a script to be run to add interactivity to the editor using Lynicon's requirement registration subsystem. In this case it shows the character count of the entered string.
...