入力フォームに他のフィールドとドロップダウンがあります。それらはすべて正常に検証されます。フロント エンドを適切に検証しない 2 つの要素でさえ、投稿では適切にモデル バインドされます。彼らが検証していない理由は完全にはわかりません。セットアップは次のとおりです。
ビューモデル
public class DataVM
{
 //Widgets used, populated from database
 public List<Widget> Widgets { get; set; } 
 //Contains data to populate a dropdown with
 public GenericSelectList Containers { get; set; }
 //This will hold the relation between the widget and container
 //where widget is the local id, and container foreign id
 public List<NestedDictionary> WidgetContainers { get; set; }
}
public class NestedDictionary
{
    [RegularExpression("/^[1-9][0-9]*$/")]
    [Required]//This is not being enforced
    public int? ForeignId { get; set; }
    public int LocalId { get; set; }
}
意見
@for (int i = 0; i < Model.Widgets.Count; i++)
{
 @Html.Hidden("WidgetContainers["+i+"].LocalId", Model.Widgets.ElementAt(i).WidgetId)
 <div>
  <div>
   <span>Container Used</span><hr />
  </div>
  <div class="editor-field">
   @Html.DropDownListFor(
    m => m.WidgetContainers.ElementAt(i).ForeignId,
    new SelectList(
     Model.Containers.Values,
     "Id",
     "DisplayFields",
     0
    ),
    " - Select A Container - "
   )
   <br />@Html.ValidationMessageFor(model => model.WidgetContainers.ElementAt(i).ForeignId)
  </div>
 </div>
}
すべてが正常に表示されます。インタラクティブで、値は正しいです。投稿されると、正しいデータが正しい場所に渡されます。ただし、Container が選択されていない場合は、値が0渡されて投稿が正常に行われます。ここで途方に暮れています。検証がクライアント側で機能しない理由はありますか?
編集
これは、非表示の入力のレンダリングされた html と select 要素です。
隠れた:
<input id="WidgetContainers_0__LocalId" type="hidden" value="39" name="WidgetContainers[0].LocalId">
選択する:
<select name="ForeignId" id="ForeignId" class="valid">
明らかに、選択には異なる属性が定義されている必要があります。
編集#2
私が使用する場合
m => m.WidgetContainers[i].ForeignId,
それから私は得る
<select id="WidgetContainers_0__ForeignId" name="WidgetContainers[0].ForeignId" class="valid">