0

私は Entity Framework を初めて使用します。Model first appraoch を使用して EDM を作成し、datannotations を使用して検証を適用しました。すべて正常に動作していますが、すべての検証は 1 か所に表示されますが、それぞれのフィールドの横に検証エラー メッセージを表示したいと思います。

私が書いたコードは以下の通りです

public partial class Hardware_services_repairs
{
public class hardwaremetadata
{
[Required]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage="Numbers and Special characters are not      allowed")]
public string CompanyName { get; set; }
[Required]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage="Numbers and Special characters are not allowed")]
public string ContactName { get; set; }
//[Required]
[RegularExpression(@"^[0-9+-]+$",ErrorMessage="Only numbers are allowed")]
public string PhoneNumber { get; set; }
//[Required]
[RegularExpression(@"^[0-9+-]+$", ErrorMessage = "Only numbers are allowed")]
public string MobileNumber { get; set; }
//[Required]
[RegularExpression(@"^[A-Za-z0-9#: ]+$", ErrorMessage = "Special Characters are not allowed")]
public string Address { get; set; }
}
}

出力は次のとおりです。

List of validation errors
Numbers and Special characters are not allowed
Numbers and Special characters are not allowed
Only numbers are allowed
Only numbers are allowed
Special Characters are not allowed

しかし、各フィールドの横にすべてのエラーメッセージを別々に表示したいので、解決策を教えてください

前もって感謝します

4

2 に答える 2

0

@Html.validationSummary を追加する必要はありません

検証するすべてのフィールドに validationMessageFor を追加し、検証エラーを表示します

<div>
    @Html.LabelFor(model => model.CompanyName) 
</div>
<div>
    @Html.EditorFor(model => model.CompanyName)
    @Html.ValidationMessageFor(model => model.CompanyName)
</div>
于 2013-07-17T06:53:40.960 に答える
0

たぶん、問題はあなたのビューにあります。

メソッド「ValidationSummary」のパラメータ「excludePropertyErrors」を「true」に設定する必要があります。

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true)

次に、各フィールドについて、次のように記述する必要があります。

<div>
    @Html.LabelFor(model => model.CompanyName) 
</div>
<div>
    @Html.EditorFor(model => model.CompanyName)
    @Html.ValidationMessageFor(model => model.CompanyName)
</div>
于 2013-06-24T16:08:10.713 に答える