MVC5 ComponentModel.DataAnnotation不起作用

时间:2015-07-04 03:20:13

标签: asp.net-mvc data-annotations

请告诉我为什么ComponentModel.DataAnnotations不能在这里工作我已经完成了所有事情但是徒劳无闻所以请告诉我这段代码中我有哪些错误。在这里,我发布了我的视图和模型。

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<form>

    <div id="par" class="form-horizontal">
        <hr />

        @Html.ValidationSummary(true)


        <div class="form-group">
            @Html.LabelFor(model => model.BrandCode, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                @Html.EditorFor(model => model.BrandCode, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.BrandCode, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.ProductSubGroupCode, new { @class = "control-label col-md-2 col-ld-2 col-sd-2" })

            <div class="col-md-10">
                @(Html.Kendo().ComboBoxFor(model => model.ProductSubGroupCode)

              .DataTextField("ProductSubGroupName")
              .DataValueField("ProductSubGroupCode")
              .DataSource(d => d.Read(r => r.Action("GetProductSubGroup", "Product")))
                      .Placeholder("Select Product Sub Group...")
                      .Suggest(true)
                      .HighlightFirst(true)
                )
                @Html.ValidationMessageFor(model => model.ProductSubGroupCode, "", new { @class = "text-danger" })
            </div>
        </div>



        <div class="form-group">
            @Html.LabelFor(model => model.ProductGroupCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @(Html.Kendo().ComboBoxFor(model => model.ProductGroupCode)
                .MinLength(100)
              .DataTextField("ProductGroupName")
              .DataValueField("ProductGroupCode")
      .DataSource(d => d.Read(r => r.Action("GetProductGroup", "Product")))
                      .Placeholder("Select Product Group...")
                      .Suggest(true)
                      .HighlightFirst(true)
                )
                @Html.ValidationMessageFor(model => model.ProductGroupCode, "", new { @class = "text-danger" })
            </div>
        </div>



        <div class="form-group">
            @Html.LabelFor(model => model.BrandName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BrandName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BrandName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BrandDescription, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BrandDescription, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BrandDescription, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.CheckBoxFor(model => model.Active)
                @Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SortOrder, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SortOrder, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SortOrder, "", new { @class = "text-danger" })
            </div>
        </div>


    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" id="Save" class="btn btn-info" />
            <p id="content"></p>
        </div>
    </div>

</form>

这是模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

public class BrandViewModels
    {

        [Display(Name = "Brand Code")]
        [Required(ErrorMessage = "Brand Code is required")]
        [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
        [Remote("IsUniqueBrandCode", "Product", AdditionalFields = "BrandCode", HttpMethod = "POST", ErrorMessage = "Brand Code already exists.")]
        public string BrandCode { get; set; }
        public int CompanyId { get; set; }
        public List<ProductSubGroupList> ProductSubGroupList { get; set; }
        public List<ProductGroupList> ProductGroupList { get; set; }

        [Required(ErrorMessage = "Please select product group")]
        [Display(Name = "Product Group")]
        public string ProductGroupCode { get; set; }


        [Required(ErrorMessage = "Please select product sub group")]
        [Display(Name = "Product Sub Group")]
        public string ProductSubGroupCode { get; set; }

        [Required(ErrorMessage = "Brand name is required")]
        [Display(Name = "Brand Name")]
        public string BrandName { get; set; }

        [Required(ErrorMessage = "Description is required")]
        [Display(Name = "Description")]
        public string BrandDescription { get; set; }

        [Display(Name = "Active")]
        public bool Active { get; set; }

        [Display(Name = "Sort Order")]
        [Required(ErrorMessage = "Sorting order is required")]
        public int? SortOrder { get; set; }

    }

2 个答案:

答案 0 :(得分:1)

请检查您是否在Web Config中有以下Appsettings

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

请参阅Data Anotation validation

答案 1 :(得分:0)

这是我的代码工作完美我看到你的代码你可能有这些错误,

  1. 您应该使用@using (Html.BeginForm())代替<form>
  2. @Html.ValidationSummary(false)代替@Html.ValidationSummary(true) 请看我的代码。

               <div class="container">
                @using (Html.BeginForm())
                {
                    @Html.AntiForgeryToken()
    
                    <div class="form-horizontal">
                        <h4>mymodel</h4>
                        <hr />
                        @Html.ValidationSummary(false)
    
                        <div class="form-group">
                            @Html.LabelFor(model => model.id, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.id)
                                @Html.ValidationMessageFor(model => model.id)
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.name, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.name)
                                @Html.ValidationMessageFor(model => model.name)
                            </div>
                        </div>
    
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Create" class="btn btn-default" />
                            </div>
                        </div>
                    </div>
                }
            </div>
            <div>
                @Html.ActionLink("Back to List", "Index")
            </div>
            <script src="~/Scripts/jquery-1.10.2.min.js"></script>
            <script src="~/Scripts/jquery.validate.min.js"></script>
            <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>