Building a Chat Web App With Signal R, Part 2: Listing 7

The KnockOut HTML personTemplate.

@model VSMSignalRPart2Demo.Models.Person

    ViewBag.Title = "Person";


<form data-bind="submit: createPerson">

    <div id="error" class="validation-summary-errors"></div>


        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        <div class="editor-field">
            @Html.TextBoxFor(model => model.FirstName,  new { @data_bind = "value: newPersonFirstName" })
            @Html.ValidationMessageFor(model => model.FirstName)

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        <div class="editor-field">
            @Html.TextBoxFor(model => model.LastName, new { @data_bind = "value: newPersonLastName" })
            @Html.ValidationMessageFor(model => model.LastName)

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Email, new { @data_bind = "value: newPersonEmail" })
            @Html.ValidationMessageFor(model => model.Email)

            <input type="submit" value="Add Person" />

<table data-bind="template: { name: 'personTemplate', foreach: people }, visible: people().length > 0">
            <th>First Name</th>
            <th>Last Name</th>

<script type="text/html" id="personTemplate">
    <!--Data Template-->
        <td><input class="ui-corner-all" data-bind="value: firstName, valueUpdate: 'afterkeydown'" /></td>
        <td><input class="ui-corner-all" data-bind="value: lastName, valueUpdate: 'afterkeydown'" /></td>
        <td><input class="ui-corner-all" data-bind="value: email, valueUpdate: 'afterkeydown'" /></td>
        <td><input class="ui-button" type="button" href="#" data-bind="click: removePerson" value="Delete" /></td>

@section Scripts {
    <script src="~/Scripts/jquery.signalR-1.0.0-rc1.min.js"></script>
    <script src="/signalr/hubs" type="text/javascript"></script>
    <script src="~/Scripts/knockout-2.1.0.js"></script>
    <script src="~/Scripts/PersonManager.js"></script>

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus


Subscribe on YouTube