我有一个页面,其中我想知道何时对任何字段进行了任何更改,然后我将禁用并启用保存按钮。
我正在使用下面的代码并且它不起作用任何人都知道我错过了什么?
@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")
}
答案 0 :(得分:0)
我认为您在此过程中过早应用绑定。尝试移动
ko.applyBindings(viewModel);
低于您的表格(即)
@using (Html.BeginForm())
{
//your form code stuff here
}
<script type="text/javascript">
ko.applyBindings(viewModel);
</script>