0

我正在尝试验证 mvc4 中表单的隐藏输入字段,但我失败了。我尝试了几种方法,但每次都失败了。我在谷歌搜索并得到了一些结果,但这些在我的情况下不起作用。我正在尝试我的代码

@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();
});

我挑衅地做错了什么,但我不明白我做错了什么。

4

1 回答 1

0

您必须执行/添加此操作才能启用验证隐藏输入:

$(document).ready(function () {
 $.validator.setDefaults({
        ignore: ""
    });
});
于 2013-07-05T02:14:36.647 回答