0

非表示フィールド (HiddenFor または非表示の EditorFor) にコントロール (dataanotation) を設定することは可能でしょうか?

そうは思いませんが、私たちは決して知りません。

EditorFor を非表示にする方法については、TextBoxFor と EditorFor、htmlAttributes と additionalViewDataなど、多くの投稿があります。

私の場合、ビューで WCF REST サービスへの jquery 呼び出しがあり、成功した場合は EditorFor を埋めます。その EditorFor に Required DataAnotation を適用したいのですが、可能でしょうか?

EditorFor が見えない限り、DataAnotation は適用できないと思います。非表示の EditorFor に DataAnotation を適用する方法はありますか?


コードは次のとおりです: EditorFor を非表示にするには:

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

カスタムエディター:

@{
    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 })

モデル:

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

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

WCF REST Service への JQuery 呼び出し:

$(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 サービス呼び出しと Required DataAnotation が適用された後に正しく入力されていることがわかります。

EditorFor を非表示にする他の方法があります。たとえば、style='width:0px;height:0px' を適用します。

Required DataAnotation を非表示にしますが無効にします。

style='width:0px;height:1px' を適用すると、多くの EditorFor は表示されませんが、Required DataAnotation はアクティブです。

4

1 に答える 1