我有一个代表一个项目的表格。该表单包含一个提交按钮。如果单击提交按钮,则应在这些字段上进行不显眼的验证。
如果验证失败,则不会发生其他任何事情。
如果验证通过,则该项目应添加到 Knockout.js 观察数组集合中。
在这两种情况下,整个过程都应该保留在客户端而不提交给服务器。提交和服务器端验证将在流程的后期进行。
我怎样才能达到预期的效果?
我正在使用带有数据注释的 ASP.Net MVC。我不想在客户端手动复制验证逻辑。
我还应该提到我在同一页面上有几个表格。
这是我到目前为止所做的......
这是我的 ASP.Net MVC 布局文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - JC Guns Online</title>
@*---------- Stylesheets ----------*@
@Styles.Render("~/Content/Bootstrap/bootstrap-theme.css")
@Styles.Render("~/Content/MightyIT/bootstrap_customizations.css")
@Styles.Render("~/Content/site.css")
@Styles.Render("~/Content/MightyIT/custom_styles.css")
@Styles.Render("~/Content/MightyIT/callout.css")
@Styles.Render("~/Content/font-awesome-4.0.3/css/font-awesome.min.css")
@RenderSection("css", required: false)
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
@*<li>
@using (Html.BeginForm())
{
<input id="txtQuickSearch" type="text" class="form-control col-lg-8" placeholder="Search">
<img src="~/Content/img/search_32.png" />
}
</li>*@
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
<div class="container body-content">
<br />
@RenderBody()
<br /><br />
<nav class="navbar navbar-default navbar-fixed-bottom">
<div style="text-align:center">
<img src="~/Content/img/logo_small.png" class="img-responsive" />
<sub style="position:absolute; right:10px; bottom:10px;">© @DateTime.Now.Year </sub>
</div>
</nav>
</div>
@*---------- Javascripts ----------*@
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/Scripts/KnockOut/knockout-3.0.0.js")
@Scripts.Render("~/Scripts/JQuery/jquery.unobtrusive-ajax.js")
@Scripts.Render("~/Scripts/JQuery/jquery.validate.js")
@Scripts.Render("~/Scripts/JQuery/jquery.validate.unobtrusive.js")
@Scripts.Render("~/Scripts/JQuery/jquery.callout.unobtrusive.js")
@Scripts.Render("~/Scripts/MVCFoolProof/mvcfoolproof.unobtrusive.js")
@RenderSection("scripts",false)
</body>
</html>
这是我目前正在处理的相关部分的代码(有几个类似的部分将放在同一页面上):
<form id="AddCrimeForm">
<div class="panel panel-success">
<div class="panel-heading">
<div class="form-horizontal">
<div class="row">
<div class="col-lg-11">Add a crime incident to the list</div>
<div class="col-lg-1">
<button type="submit" class="btn btn-success btn-xs" onclick="addCrime();"><i class="fa fa-plus"></i> Add</button>
</div>
</div>
</div>
</div>
<div class="panel-body">
<div class="form-horizontal">
<div class="row">
<div class="col-lg-6">
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="">
<div class="form-group">
<label class="control-label col-md-4" for="CaseNumber">Case Number</label>
<div class="col-md-8">
<input class="form-control text-box single-line" data-val="true" data-val-required="The Case Number field is required." id="CaseNumber" name="CaseNumber" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="CaseNumber" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="DateOfIncident">Date Of Incident</label>
<div class="col-md-8">
<input class="form-control text-box single-line valid" data-val="true" data-val-required="The Date of Incident field is required." id="DateOfIncident" name="DateOfIncident" type="date" value="">
<span class="field-validation-valid" data-valmsg-for="DateOfIncident" data-valmsg-replace="true"></span>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="control-label col-md-4" for="Description">Description</label>
<div class="col-md-8">
<textarea class="form-control text-box multi-line" data-val="true" data-val-required="The Description field is required." id="Description" name="Description"></textarea>
<span class="field-validation-valid" data-valmsg-for="Description" data-valmsg-replace="true"></span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<table class="table table-striped table-hover " id="CrimeList">
<thead>
<tr>
<th>Case Number</th>
<th>Date of Incident</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach: items">
<tr>
<td data-bind="text: $data.CaseNumber">Column content</td>
<td data-bind="text: $data.DateOfIncident">Column content</td>
<td data-bind="text: $data.Description" style="text-wrap: normal">Column content</td>
@*<td></td>
<td></td>
<td></td>*@
<td>...</td>
</tr>
</tbody>
</table>
这是 client_crime_kjs.js 的代码,以及我所有的 KnouckoutJS 视图模型代码:
$(document).ready(
function ()
{
var Crime = function(CaseNumber, DateOfIncident, Description)
{
this.CaseNumber = CaseNumber;
this.DateOfIncident = DateOfIncident;
this.Description = Description;
}
var initialData = new Array();
var crimes = function (items)
{
var self = this;
//Data
self.items = ko.observableArray(items)
//operations
self.addCrime = function()
{
if ($("#AddCrimeForm").valid()) {
self.crime = new Crime($("#CaseNumber").val(), $("#DateOfIncident").val(), $("#Description").val());
//var JSONObj = { CaseNumber: $("#CaseNumber").val(), DateOfIncident: $("#DateOfIncident").val(), Description: $("#Description").val() };
self.items.push(this.crime);
}
//$("#CaseNumber").val() = "";
//$("#DateOfIncident").val() = "";
//$("#Description").val() = "";
}
}
ko.applyBindings(crimes(initialData), $("#CrimeList")[0])
}
);
基本上在这个阶段发生的情况是,当字段无效时,表单不会提交(正确如此),但是当它验证时它确实提交(与我的要求相反),并且我的 KO observablearray 随后重置。