我想知道是否可以在隐藏字段(HiddenFor 或 hidden EditorFor)上有控件(dataanotation)?
我不这么认为,但我们永远不知道。
有很多关于如何隐藏 EditorFor 的帖子,例如: TextBoxFor vs EditorFor,以及 htmlAttributes vs additionalViewData
就我而言,在一个视图中,我有一个对 WCF REST 服务的 jquery 调用,在成功的情况下填充我的 EditorFor。我希望在该 EditorFor 上应用所需的 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 服务的 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 服务调用并应用了所需的 DataAnotation 后被正确填充。
还有其他方法可以隐藏 EditorFor,例如应用 style='width:0px;height:0px'
它隐藏但禁用了必需的 DataAnnotation,
如果我应用 style='width:0px;height:1px',我们看不到很多 EditorFor,但必需的 DataAnotation 是活动的。