0

I wonder if it's possible to have controls (dataanotation) on hidden fields (HiddenFor or hidden EditorFor) ?

I don't think so, but we never know.

There are a lot of posts on how to hide EditorFor such as : TextBoxFor vs EditorFor, and htmlAttributes vs additionalViewData

In my case,in a view I have a jquery call to a WCF REST service, that in success case fill my EditorFor. I would like that the Required DataAnotation to be applied on that EditorFor, would it be possible ?

I think that as long as the EditorFor is invisible the DataAnotation cannot be applied. Would it have a way to apply the DataAnotation on the hidden EditorFor ?


Here is the code : To hide the EditorFor :

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

The 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 })

the model :

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

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

The JQuery call to WCF REST Service :

$(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
    });
});

If I don't hide the EditorFor I can see it is correctly filled after the WCF REST service call and the Required DataAnotation is applied.

There are other way to hide the EditorFor, for instance to apply the style='width:0px;height:0px'

It hides but disable the Required DataAnotation,

if I apply the style='width:0px;height:1px', we don't see a lot of the EditorFor but the Required DataAnotation is active.

3
  • 1
    I think that DataAnnotation is applied to field no matter if EditorFor is hidden or not. Can you post some code that is not working, your ViewModel and View? Commented Nov 4, 2013 at 12:27
  • Yes the validation would still work. Commented Nov 4, 2013 at 13:16
  • The validation doesn't seem to work on a hiddenfield, would you have some examples where validation works with an HiddenFor ? Commented Nov 5, 2013 at 17:08

1 Answer 1

0

I've seen an answer at http://www.campusmvp.net/blog/validation-of-hidden-fields-at-the-client-in-asp-net-mvc

(but it seems i had badly searched precedently, the validation of hidden field is treated in some blogs and sites).

To active the validation of hidden fields, you just have to add this little javascript line :

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

and it works !

Apparently it doesn't work with mvc2, but works since mvc3.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.