I'm trying to make some Category entity adding form with unobstrusive validation on client side.
Here is my entity:
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; }
...
}
Here is my view:
@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>
}
Now I left all my inputs emty and tried to validate this form to get the errors shown in my model using:
$("#addCategoryForm").validate().valid()
But I get true
(and no errors) as a result, but should be false
because in the data annotation I added [Required]
attribute for few fields. What I'm doing wrong?
Thanks for any advance.
EDIT-1
Here is my config:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Here is references:
<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>