私はこれに従いますpost。xVal - IDフィールド専用のルールを生成します
サーバー側の検証は、期待通りに機能します。しかし、クライアント側のバリデータは、IDフィールドに対してのみ生成されます。
私Linq2Sqlエンティティークラスは二つのプロパティID &区分名を持っており、以下の私のメタデータクラスは、カテゴリ
/// <summary>
/// Adds the category.
/// </summary>
/// <param name="category">The category.</param>
public void AddCategory(BookCategory category)
{
var errors = DataAnotationsValidationHelper.GetErrors(category);
if (errors.Any()) {
throw new RulesException(errors);
}
_db.BookCategories.InsertOnSubmit(category);
_db.SubmitChanges();
}
を追加する
[MetadataType(typeof(BookCategoryMetadata))]
public partial class BookCategory{}
public class BookCategoryMetadata
{
[Required]
[StringLength(50)]
public string CategoryName { get; set; }
}
方法であるコントローラ
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ID")]BookCategory category)
{
try {
_repository.AddCategory(category);
} catch (RulesException ex) {
ex.AddModelStateErrors(ModelState, "");
}
return ModelState.IsValid ? RedirectToAction("Index") : (ActionResult)View();
}
でアクションを作成します。
そしてビュー
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="CategoryName">CategoryName:</label>
<%= Html.TextBox("CategoryName") %>
<%= Html.ValidationMessage("CategoryName", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
<%= Html.ClientSideValidation<BookCategory>() %>
今XVALは、IDフィールドの検証ルールを生成します。
<script type="text/javascript">xVal.AttachValidator(null, {"Fields":[{"FieldName":"ID","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]}]})</script>
CategoryNameのサーバー側の検証は完全に機能します。 xValがCategoryNameの検証規則を生成しないのはなぜですか?私は間違って何をしていますか?
最後のコメントに基づいて回答してください。ありがとう。 – andymeadows