0

我有一个页面,我想知道何时对任何字段进行了任何更改,然后我将禁用并启用保存按钮。

我正在使用下面的代码,它不起作用有人知道我缺少什么吗?

@using CirrusAdmin.Models
@model MedicineModel
@{
    ViewBag.Title = "Update Medicine";
}

<script src="@Url.Content("~/Scripts/knockout-2.1.0.js")" type="text/javascript"></script>

<script type="text/javascript">
    var initialData = @Html.Raw(Json.Encode(Model));

    var viewModel = {
                 item : ko.observable(initialData),
                 isDirty : ko.DirtyFlag(item)
    };

    ko.applyBindings(viewModel);
</script>


<hgroup class="title">
    <h1>@ViewBag.Title.</h1>    
</hgroup>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    <fieldset>        
        <ol>
            <li>
                @Html.LabelFor(m => m.Code)
                @Html.TextBoxFor(m => m.Code, new { data_bind= "value: Code", @readonly = "readonly" })
            </li>
            <li>
                @Html.LabelFor(m => m.Name)
                @Html.TextBoxFor(m => m.Name, new { data_bind = "value: Name" })
            </li>
            <li>
                @Html.LabelFor(m => m.GenericName)
                @Html.TextBoxFor(m => m.GenericName, new { data_bind = "value: GenericName" })
            </li>
        </ol>
    </fieldset>

    <input type="submit" data-bind="enable: isDirty" value="Save" />
}

    @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
4

1 回答 1

0

我认为您在此过程中太早应用绑定。尝试移动

 ko.applyBindings(viewModel);

在您的表格下方(即)

@using (Html.BeginForm()) 
{
  //your form code stuff here
}
<script type="text/javascript">
 ko.applyBindings(viewModel);
</script>
于 2013-01-16T04:20:11.657 回答