5

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>
    }

ただし、フォームが送信されたときのみNameEmail検証エラーが返されMessageます。

ここに画像の説明を入力

Messageからに変更するTextAreaForTextBoxFor、検証は正しく機能します。

これはなぜですか? また、テキスト ボックスで検証を機能させるにはどうすればよいですか?


また、このフォーム用に特定の jQuery を作成する必要がなかったことも注目に値するかもしれません。MVC3によってすべて処理されるため、これは必要ないことを説明するチュートリアルに従いました。

4

3 に答える 3

11

対応するモデル プロパティを[DataType(DataType.MultilineText)]データ アノテーションで装飾し、Html.EditorForヘルパー メソッドを使用します。

[Required]
[DataType(DataType.MultilineText)]
public string Message { get; set; }

@Html.EditorForヘルパーメソッドを使用 する

@Html.EditorFor(m => m.RoleDescription)
于 2012-05-04T20:18:34.460 に答える
1

モデルがネストされているときの TextAreaFor のバグです。CommentModel の内容をいわば「ルート」モデルに配置すると、すべてがうまく機能します。または、TextAreaFor を TextBoxFor に変更すると、うまく機能します。

無関係なトピックについてですが、あなたのようにコメント モデルを入れ子にしようとしているのは私だけではないと確信しています。これは本当に直すべきです!

編集: 問題のバグチケットは次のとおりです。

http://aspnet.codeplex.com/workitem/8576

Edit2: EditorFor を使用する Shyju の回避策が機能します! 興味深いことに、いくつかのクラス名が の出力に追加されるため、CSS と競合しないように注意してください。

于 2012-05-04T20:14:18.363 に答える