无法获得针对淘汰赛和MVC的语法

时间:2012-10-15 13:45:31

标签: asp.net-mvc knockout.js

我第一次尝试淘汰赛,我正努力让它发挥作用。 基本方案是我想使用自动完成功能将员工添加到列表中。我希望能够删除它。当用户满意时,他可以点击提交,这将把ID列表发布到服务器,我将进入下一部分流程。 目前我正在尝试让添加员工先行。

我有一个服务器端视图模型类;

public class TrainingListEmployeesViewModel : BaseTrainingViewModel
{
    public TrainingListEmployeesViewModel()
    {
        this.EmployeeList = new List<int>();
        this.ClientEmployeeSelector = new ClientEmployeeSelector();
    }

    public TrainingListEmployeesViewModel(List<int> employeeList, string trainingName, string trainingDescription)
        : base(trainingName, trainingDescription)
    {
        this.EmployeeList = employeeList;
        var dates = new CalendarDates(Utility.GetToday(), Utility.GetToday().AddYears(1));
        this.Cvm = new CalendarViewModel(employeeList.ToList(), dates);
    }

    public List<int> EmployeeList { get; set; }

    public ClientEmployeeSelector ClientEmployeeSelector { get; set; }
}

}

控制器看起来像这样;

[HttpPost]
        [Authorize(Roles = "Administrator, Trainer, ManagerAccounts, ManagerIT")]
        [ValidateAntiForgeryToken]
        [ValidateOnlyIncomingValuesAttribute]
        public ActionResult Training(TrainingViewModel tvm)
        {
            SessionObjects.TrainingName = tvm.TrainingName;
            SessionObjects.TrainingDescription = tvm.TrainingDescription;
            return this.RedirectToAction("Training", new { employeeId = tvm.EmployeeSelector.SearchTextId });
        }

        [HttpGet]
        [Authorize(Roles = "Administrator, Trainer, ManagerAccounts, ManagerIT")]
        public ActionResult BulkTraining()
        {
            return this.View(new TrainingListEmployeesViewModel());
        }

视图看起来像这样;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SHP.Models.TrainingListEmployeesViewModel>" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Bulk Training
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form class="employeeListEditor">
    <h3>Allocate or cancel training for the selected employees</h3>
    <table>
        <tr>
            <td style="text-align: right;">Course Name</td>
            <td><%: Html.EditorFor(model => model.TrainingName) %></td>
        </tr>
        <tr>
            <td style="text-align: right;">Description (optional)</td>
            <td><%: Html.TextAreaFor(
               model => model.TrainingDescription, new { maxlength = "255", style = "width:400px;height:100px;" }) %></td>
        </tr>
    </table>
    <%: Html.EditorFor(model => model.ClientEmployeeSelector) %>
    <button data-bind="click: addEmployee">Add</button>
    <div id="displayEmployees" style="margin-top:10px;display: block">
        <table id="employeeDataTable" class="groupBorder">
            <thead>
                <tr>
                    <th></th>
                    <th>Employee</th>
                </tr>
            </thead>
            <tbody data-bind="foreach: employees">
                <tr>
                    <td>
                        <a href="#" data-bind="click: function() { viewModel.removeEmployee($data) }">Remove</a>
                        <input type="hidden" data-bind="value: searchTextId"/>
                    </td>
                    <td><span data-bind="value: searchText"></span></td>
                </tr>               
            </tbody>
        </table>
    </div>
</form>

    <script type="text/javascript">
        function Employee(id, text) {
            var self = this;
            self.searchId = id;
            self.searchText = text;
        }

        var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>;
        function ViewModel() {
            var self = this;
            self.employees = ko.observableArray(initialData.EmployeeList);
            self.searchText = ko.observable(initialData.SearchText);
            self.searchTextId = ko.observable(initialData.SearchTextId);
            self.removeEmployee = function(employee) {
                this.employees.remove(employee);
            };
            self.addEmployee = function() {
                alert(self.searchText);
                this.employees.push(new Employee(self.searchTextId, self.searchText));
            };
            self.save = function() {
                ko.utils.postJson(location.href, { employees: self.employees });
            };
        }
        ko.applyBindings(new ViewModel());
    </script>
</asp:Content>

编辑器模板看起来像ClientEmployeeSelector,它包含自动完成;

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.Models.ClientEmployeeSelector>" %>

        <table class="groupBorder">
            <tr>
                <th colspan="2">Enter 2 or more characters for "<%: Html.LabelFor(model => model.SearchText)%>" and select from the list.</th>
            </tr>
            <tr>
                <td><%: Html.LabelFor(model => model.SearchText)%></td>
                <td><%: Html.AutocompleteFor(model => model.SearchText, controller: "Payroll",
                                            minLength: 2, maxResults: 10,
                                            htmlAttributes: new { style = "color:purple; width:500px;" })%>
                                <%: Html.HiddenFor(model => model.SearchTextId)%>
                                <%: Html.HiddenFor(model => model.SearchText)%>
                </td>
            </tr>
        </table>

我收到错误消息; searchTextId未定义。当我查看IE开发人员工具中发生的事情时,searchTextId存在,但是无法找到它的值。那我怎么能让这个工作?我怀疑我需要做一些改变。

1 个答案:

答案 0 :(得分:1)

在我看来,你绑定了错误的属性。如果要与员工一起显示表,则应在foreach绑定中使用员工的属性。最好在绑定中使用$parent$root个对象,而不是使用viewModel对象:

    <tbody data-bind="foreach: employees">
        <tr>
            <td>
                <a href="#" data-bind="click: $parent.removeEmployee">Remove</a>
                <input type="hidden" data-bind="value: searchId"/>
            </td>
            <td><span data-bind="text: searchText"></span></td>
        </tr>               
    </tbody>

这是工作小提琴:http://jsfiddle.net/vyshniakov/yXvDB/2/

但是如果你真的想在employees表中显示视图模型的searchTextId属性,你应该使用$parent对象:

<input type="hidden" data-bind="value: $parent.searchTextId"/>