我有一个MVC剃刀视图,其中包含我的模型中定义的属性的验证规则。 由于我想将partialView返回到我的页面,我做了一个“自定义”提交函数来调用:
function SubmitNewstock() {
var form = $("form");
form.validate();
alert("Valid: " + form.valid());
}
这就是我得到的:
首先我收到警告说它.valid()是真的。 SECOND我得到了页面中的验证错误。
我也尝试过: $ .validator.unobtrusive.parse( “形式”);
查看:
@model EstudioVenta.Stock
....
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group col-md-4">
@Html.LabelFor(model => model.Codigo_, htmlAttributes: new { @class = "col-md-5 control-label" })
<div class="col-md-7">
@Html.TextBoxFor(model => model.Codigo_, htmlAttributes: new { @class = "form-control", @placeholder = "Codigo del Articulo" })
@Html.ValidationMessageFor(model => model.Codigo_)
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(model => model.Nombre_, htmlAttributes: new { @class = "col-md-5 control-label" })
<div class="col-md-7">
@Html.TextBoxFor(model => model.Nombre_, htmlAttributes: new { @class = "form-control", @placeholder = "Nombre de Articulo" })
@Html.ValidationMessageFor(model => model.Nombre_)
</div>
</div>
<div class="form-group col-md-2">
@Html.LabelFor(model => model.Cantidad, htmlAttributes: new { @class = "col-md-5 control-label" })
<div class="col-md-7">
@Html.TextBoxFor(model => model.Cantidad, htmlAttributes: new { @class = "form-control", @placeholder = "Catidad de Articulos" })
@Html.ValidationMessageFor(model => model.Cantidad)
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(model => model.PrecioCosto_, htmlAttributes: new { @class = "col-md-5 control-label" })
<div class="col-md-7">
@Html.TextBoxFor(model => model.PrecioCosto_, htmlAttributes: new { @class = "form-control", @placeholder = "Precio de Costo del Articulo" })
@Html.ValidationMessageFor(model => model.PrecioCosto_)
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(model => model.PrecioVenta_, htmlAttributes: new { @class = "col-md-5 control-label" })
<div class="col-md-7">
@Html.TextBoxFor(model => model.PrecioVenta_, htmlAttributes: new { @class = "form-control", @placeholder = "Precio de Venta del Articulo" })
@Html.ValidationMessageFor(model => model.PrecioVenta_)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Agregar Stock" class="btn btn-default btn-primary btn-block" onclick="SubmitNewstock()" />
</div>
</div>
</div>
@Html.HiddenFor(model => model.FechaDeEntrada_)
}
....
<script>
function SubmitNewstock() {
var form = $("form");
$.validator.unobtrusive.parse("form");
form.validate();
alert("Valid: " + form.valid());
}
$(function () {
//Save the browser time in a hidden
$("#FechaDeEntrada_").val(GetClienTime());
setInterval(function () {
$("#FechaDeEntrada_").val(GetClienTime());
}, 10000); //Do it again every 10 sec
//DATATABLES
var table = $('#stockTable').DataTable({
"pageLength": 25,
language: {
search: "Buscar:",
decimal: ",",
paginate: {
first: "Primera",
previous: "Anterior",
next: "Siguiente",
last: "Ultima"
},
}
});
});
</script>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
如果页面对提交无效,为什么我将.valid()设为true?
答案 0 :(得分:0)
请检查您在页面上有多少表格。
为您的表单指定ID,然后尝试
var form = $(&#34; #myform&#34;);