Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
languagexml
@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().

...