私はこのようなオブジェクトを持っています:
public class Test
{
public String TestValue { get; set; }
}
このオブジェクトには、テンプレート用のカスタムエディタがあります。
@inherits System.Web.Mvc.WebViewPage<MvcApplication12.Models.TestModel>
@Html.EditorFor(m => m.TestValue)
およびモデルバインダー:
public class TestBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult providerValue =
bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".TestValue");
bindingContext.ModelState.SetModelValue(bindingContext.ModelName + ".TestValue", providerValue);
if (null != providerValue && !String.IsNullOrWhiteSpace(providerValue.AttemptedValue))
{
Test test = new Test();
test.TestValue = providerValue.AttemptedValue;
return test;
}
return null;
}
}
コントローラのモデルは次のようになります。
public class LogOnModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Value")]
public Test Value { get; set; }
}
ご覧のとおり、上記のテンプレートのカスタムエディタによってレンダリングされるTestオブジェクトを使用しています。
かみそりの構文は次のようなものです。
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Value)
</div>
<div class="editor-field">
@Html.EditorFor(m => m.Value)
@Html.ValidationMessageFor(m => m.Value)
</div>
モデルのデータ注釈は、テストオブジェクト(m.Value)の入力が必要であることを示しています。このフィールドに入力がない場合、ModelBinder(TestBinder)はnullを返します。
次に、検証メッセージが次のように表示されます。
ただし、「input-validation-error」と呼ばれるcssクラスは入力フィールドに追加されません。
モデルエラーでmvc3がカスタムエディターテンプレートのすべてのネストされた入力フィールドにcssクラス「input-validation-error」を追加することをどのように達成できますか?