0

我正在尝试在客户端创建一些具有不显眼验证的类别实体添加表单。

这是我的实体:

public class Category
{
    public Int32 Id { get; set; }

    [DisplayName("Alias")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Have to complete")]
    [StringLength(100, ErrorMessage = "asdasdasd", MinimumLength = 3)]
    public String Name { get; set; }

    [DisplayName("Name1")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Have to complete")]
    public String DisplayName { get; set; }

    [DisplayName("Name2")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Have to complete")]
    public String DisplayNameTZK { get; set; }

    [DisplayName("Url")]
    [DataType(DataType.Url, ErrorMessage = "Url")]
    public String Uri { get; set; }

    public Guid AddingGuid { get; set; }

    public Boolean IsActive { get; set; }

   ...
}

这是我的看法:

@model HSDT.Models.Entities.Category

@{
    Html.EnableUnobtrusiveJavaScript();
    Html.EnableClientValidation();
}

@using (Html.BeginForm("", "", null, FormMethod.Post, new { id = "addCategoryForm", name = "addCategoryForm" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false)

    <fieldset>
        <legend>Category</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DisplayName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DisplayName)
            @Html.ValidationMessageFor(model => model.DisplayName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DisplayNameTZK)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DisplayNameTZK)
            @Html.ValidationMessageFor(model => model.DisplayNameTZK)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Uri)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Uri)
            @Html.ValidationMessageFor(model => model.Uri)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AddingGuid)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AddingGuid)
            @Html.ValidationMessageFor(model => model.AddingGuid)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.IsActive)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IsActive)
            @Html.ValidationMessageFor(model => model.IsActive)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

现在,我将所有输入都留空,并尝试验证此表单以获取模型中显示的错误:

$("#addCategoryForm").validate().valid()

但是我得到true了(并且没有错误),但应该是false因为在数据注释中我[Required]为几个字段添加了属性。我做错了什么?

感谢您的任何提前。

编辑-1

这是我的配置:

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

这里是参考:

    <script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
4

2 回答 2

1

您是否以正确的顺序添加了 js 文件?

<script src="/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>

在 webconfig 中:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
于 2013-06-03T11:12:57.943 回答
-1

从您的视图中删除以下内容

$("#addCategoryForm").validate().valid()
Html.EnableUnobtrusiveJavaScript();
Html.EnableClientValidation();
@Html.ValidationSummary(false)

将以下 js 文件添加到您的视图中:

<script type="text/javascript" src="../../Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.unobtrusive.min.js"></script>

利用

<button type="submit">Create</button> 

代替

<input type="submit" value="Create" />

按照@Lasse 的描述添加Web-config 部分。这将导致您模型验证抛出验证错误(如果有)。

于 2013-06-03T11:55:40.630 回答