2013-11-04 5 views
0

숨겨진 필드 (HiddenFor 또는 hidden EditorFor)에서 컨트롤 (데이터 주석)을 사용할 수 있는지 궁금합니다.MVC4 .Net 숨겨진 필드의 컨트롤

저는 그렇게 생각하지 않지만 우리는 결코 알지 못합니다. 뷰에서 내가 성공 케이스에 내 EditorFor을 채우하는 WCF REST 서비스에 JQuery와 통화를 가지고, 내 경우 TextBoxFor vs EditorFor, and htmlAttributes vs additionalViewData

:

같은 EditorFor 숨기는 방법에 대한 글이 많이 있습니다. 필요한 DataAnotation을 해당 EditorFor에 적용하려면 가능한 것이 좋습니까?

EditorFor가 보이지 않는 한 DataAnotation을 적용 할 수 없다고 생각합니다. 숨겨진 EditorFor에 DataAnotation을 적용 할 수있는 방법이 있습니까? EditorFor을 숨기려면 : 여기


코드입니다

@Html.EditorFor(model => model.VilleDepart, "CustomEditor", new {style = "display:none;" }) 

CustomEditor :

@{ 
    string s = ""; 
    if (ViewData["style"] != null) { 
     // The ViewData["name"] is the name of the property in the addtionalViewData... 
     s = ViewData["style"].ToString(); 
    } 
} 

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { style = s }) 

모델 : WCF에

string _VilleDepart; 
[Required] 
[Display(Name = "Ville Départ")] 
public string VilleDepart 
{ 
    get 
    { 
     if (Commune != null) { 
      return Commune.Commune1; 
     } 

     return _VilleDepart; 
    } 
    set { 
     _VilleDepart = value; 
    } 
} 

JQuery와 통화 REST 서비스 :

$(document).ready(function() { 
    $([document.getElementById("IVilleDepart"), document.getElementById("IVilleArrivee")]).autocomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       cache: false, 
       type: "GET", 
       async: false, 
       dataType: "json", 
       url: GetSearchCommunetURl + "(" + request.term + ")", 
       success: function (data) { 
        //alert(data); 
        response($.map(data, function (item) { 
         return { 
          label: item['Commune'] + ' (' + item['CodePostal'] + ')', 
          val: item 
         } 
        })) 
       }, 
       error: function (response) { 
        alert("error ==>" + response.statusText); 
       }, 
       failure: function (response) { 
        alert("failure ==>" + response.responseText); 
       } 
      }); 
     }, 
     select: function (e, i) { 
      if (e.target.id == "IVilleDepart") { 
       VilleDepart = i.item.val; 
       EVilleDepart.value = VilleDepart.Commune; 
       ECodePostalDepart.value = VilleDepart.CodePostal; 
       ECodeINSEEDepart.value = VilleDepart.CodeINSEE; 

      } 
      if (e.target.id == "IVilleArrivee") { 
       VilleArrivee = i.item.val; 
       EVilleArrivee.value = VilleArrivee.Commune; 
       ECodePostalArrivee.value = VilleArrivee.CodePostal; 
       ECodeINSEEArrivee.value = VilleArrivee.CodeINSEE; 
      } 
     }, 
     minLength: 2 
    }); 
}); 

나는 EditorFor을 숨기지 않으면 내가 올바르게 WCF REST 서비스 호출 후 작성하고 필수 DataAnotation이 적용되는 볼 수 있습니다. 인스턴스가 스타일을 = 적용하는

는 EditorFor을 숨길 수있는 다른 방법이있다 '폭 : 0 픽셀, 높이 : 0 픽셀'

그것은하지만 필수 DataAnotation를 사용하지 않도록 숨 깁니다,

나는 스타일을 적용하는 경우 = 'width : 0px, height : 1px', EditorFor는 많이 보이지 않지만 필수 DataAnotation은 활성화되어 있습니다.

+1

EditorFor가 숨겨져 있든 상관없이 DataAnnotation이 필드에 적용된다고 생각합니다. ViewModel과 View가 작동하지 않는 코드를 게시 할 수 있습니까? – freshbm

+0

예, 유효성 검사가 계속됩니다. – WannaCSharp

+0

유효성 검사가 숨겨진 필드에서 작동하지 않는 것 같습니다. 유효성 검사가 HiddenFor와 작동하는 몇 가지 예가 있습니까? – fguigui

답변

0

나는 http://www.campusmvp.net/blog/validation-of-hidden-fields-at-the-client-in-asp-net-mvc

에서 답을 보았다 (하지만 내가 심하게 먼저 인 검색했다, 숨겨진 필드의 유효성 검사가 일부 블로그와 사이트에서 처리 보인다).

$.validator.setDefaults({ ignore: null }); 

을하고 그것을 작동 : 활성으로

숨겨진 필드의 검증, 당신은이 작은 자바 스크립트 줄을 추가해야합니다!

분명히 mvc2에서는 작동하지 않지만 mvc3 이후에는 작동합니다.

관련 문제