[英]How to Validating hidden input field of a form using jquery in MVC 4?
I am trying to Validated hidden input fields of a form in mvc4 but i failed. 我正在尝试验证mvc4中表单的隐藏输入字段,但失败了。 I try several ways but every time i failed.
我尝试了几种方法,但是每次失败。 I searched in google and get some result but those are not work in my case.
我在google中搜索并获得了一些结果,但对于我而言,这些结果不起作用。 There is my code which I trying
我正在尝试的代码
@using (Html.BeginForm("NewSku","Product"))
{
@Html.AntiForgeryToken()
<label>Company Name :</label>
@(Html.Kendo().ComboBoxFor(x => x.CompanyId)
.Name("CompanyId")
.DataValueField("Id")
.DataTextField("CompanyName")
.Placeholder("Select Company")
.Filter(FilterType.Contains)
.DataSource(source => source.Read(read => read.Action("GetAllCompanys", "Product"))))
<br />
<label>Brand Name :</label>
@(Html.Kendo().ComboBoxFor(x => x.BrandId)
.Name("BrandId")
.DataTextField("BrandName")
.DataValueField("Id")
.Placeholder("Select Brand")
.DataSource(source => source.Read(read => read.Action("GetAllBrandsByCompany", "Product").Data("filterBrand")).ServerFiltering(true))
.AutoBind(false)
.CascadeFrom("company"))
<br />
<label>Product Name :</label>
@(Html.Kendo().ComboBoxFor(x => x.ProductId)
.HtmlAttributes(new { style = "Id=ProductComboBox" })
.Name("ProductId")
.DataTextField("ProductName")
.DataValueField("Id")
.Placeholder("Select Product")
.DataSource(source => source.Read(read => read.Action("GetAllProductsByBrand", "Product").Data("filterProduct")).ServerFiltering(true))
.AutoBind(false)
.CascadeFrom("brand"))
<br />
<label>Sku Name :</label>
@(Html.Kendo().ComboBoxFor(x => x.SkuId)
.Name("SkuId")
.HtmlAttributes(new { style = "Id=SkuComboBox" })
.DataTextField("SkuName")
.DataValueField("Id")
.Placeholder("Select Sku")
.DataSource(source => source.Read(read => read.Action("GetAllSkusByProduct", "Product").Data("filerSku")).ServerFiltering(true))
.AutoBind(false)
.CascadeFrom("product"))
<br />
<span id="ErrorMessagess" class="alert-error">
</span>
@Html.HiddenFor(x => x.CompanyName)
@Html.ValidationMessageFor(x=>x.CompanyName)
@Html.HiddenFor(x => x.BrandName)
@Html.ValidationMessageFor(x=>x.BrandName)
@Html.HiddenFor(x => x.ProductName)
@Html.ValidationMessageFor(x=>x.ProductName)
@Html.HiddenFor(x => x.SkuName)
@Html.ValidationMessageFor(x=>x.SkuName)
<input type="submit" class="k-button keep-right" value="Update" />
@Html.ValidationSummary(true)
}
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script>
$(document).ready(function () {
//$('form').validate().settings.ignore = [];
//$('form').validate({
// rules: {
// CompanyName: {
// required: true
// },
// BrandName: {
// required: true
// }
// }
//});
});
$('form').submit(function (e) {
var validator = $('form').data('validator');
validator.settings.ignore = "";
var errorList = $('form').validate().errorList;
console.log(errorList);
$("form").validate().form();
var form = $(this);
$("#CompanyName").val($("#CompanyId").data("kendoComboBox").text());
$("#BrandName").val($("#BrandId").data("kendoComboBox").text());
$("#ProductName").val($("#ProductId").data("kendoComboBox").text());
$("#SkuName").val($("#SkuId").data("kendoComboBox").text());
$.ajax({
url: form.attr("action"),
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify($('form').serializeJSON()),
success: function (result) {
// console.log(result);
$("#ErrorMessagess").val(result);
return;
}
});
e.preventDefault();
});
I am defiantly doing something wrong but I am does not understand what I am doing wrong. 我挑衅地做错了事,但是我不明白我在做什么错。
You must do/add this in order to enable to validate hidden input: 您必须执行/添加此操作才能启用验证隐藏的输入:
$(document).ready(function () {
$.validator.setDefaults({
ignore: ""
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.