2
다른 필드와 드롭 다운이 입력 양식에 있습니다. 그들은 모두 유효성을 검사합니다. 프론트 엔드를 적절하게 검증하지 못하는 두 요소조차도 게시물에서 여전히 올바르게 바인딩을 모델링합니다. 나는 그들이 왜 검증을하지 않는지 완전히 확신하지 못합니다.DropDownList 유효성 검사에 문제가있는 복잡한 모델의 경우
뷰 모델
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
값이 전달됩니다. 여기서 손실의 종류. 유효성 검사가 클라이언트 측에서 작동하지 않는 이유는 무엇입니까? 분명히 선택
<select name="ForeignId" id="ForeignId" class="valid">
:
<input id="WidgetContainers_0__LocalId" type="hidden" value="39" name="WidgetContainers[0].LocalId">
가 선택
편집
숨겨진 입력 렌더링 된 HTML 및
숨겨진 선택 요소 다른 속성을 가져야합니다. de 벌금. 내가
m => m.WidgetContainers[i].ForeignId,
를 사용하는 경우가 전혀 null이 아니니까
편집 # 2
는 내가
<select id="WidgetContainers_0__ForeignId" name="WidgetContainers[0].ForeignId" class="valid">
의 드롭 다운을 만들 때하지
0
것을 이것은 잘못된 것입니다 당신이ForeignId
가 null이 될 수 있도록해야 작동합니다. 0은 기본값이 설정되었음을 보여줍니다. 그럼에도 불구하고 누군가가 null 옵션을 선택하면이 필드는 유효하지 않은 것으로 표시되어야합니다. 0 대신에 1이 될 수 있습니다. 사용자가'' "'값을 가진 title 옵션을 선택한 경우 유효성 검사가 시작됩니다. 또한 select 요소에 대한 데이터 속성이 완전히없는 것에 유의하십시오. –나는 당신이 null 값으로 일부 DropDown Itmes를 가졌다는 것을 몰랐다. 그런데 그것이 문제라고 생각했다. Sorry :) –