ASP.NET MVC3 Razor で「jQuery Validate Unobtrusive」を使用しています。
次のような「コメント」フォームのあるページがあります。
モデル
public class CommentModel
{
[Required]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[DataType(DataType.Url)]
[Display(Name = "Website URL")]
public string WebsiteUrl { get; set; }
[Required]
public string Message { get; set; }
}
意見
@using (Html.BeginForm("AddComment", "Blog", new { @articleID = article.ID }))
{
<p>
@Html.LabelFor(m => m.commentModel.Name)
@Html.TextBoxFor(m => m.commentModel.Name)
@Html.ValidationMessageFor(m => m.commentModel.Name)
</p>
<p>
@Html.LabelFor(m => m.commentModel.Email)
@Html.TextBoxFor(m => m.commentModel.Email)
@Html.ValidationMessageFor(m => m.commentModel.Email)
</p>
<p>
@Html.LabelFor(m => m.commentModel.WebsiteUrl)
@Html.TextBoxFor(m => m.commentModel.WebsiteUrl)
@Html.ValidationMessageFor(m => m.commentModel.WebsiteUrl)
</p>
<p>
@Html.LabelFor(m => m.commentModel.Message)
@Html.TextAreaFor(m => m.commentModel.Message)
@Html.ValidationMessageFor(m => m.commentModel.Message)
</p>
<p>
<input type="submit" value="Submit Comment" />
</p>
}
ただし、フォームが送信されたときのみName
、Email
検証エラーが返されMessage
ます。
Message
からに変更するTextAreaFor
とTextBoxFor
、検証は正しく機能します。
これはなぜですか? また、テキスト ボックスで検証を機能させるにはどうすればよいですか?
また、このフォーム用に特定の jQuery を作成する必要がなかったことも注目に値するかもしれません。MVC3によってすべて処理されるため、これは必要ないことを説明するチュートリアルに従いました。