0

I have been working very hard on several very long views which are attached to my models through "ViewModel" intermediaries. My model is in a separate project.

All works fine but some of the fields in the view I want to hide. I have used for the most part, something like this:

        <td id="Left">@Html.LabelFor(x=>x.Id)</td>
        <td id="Right">@Html.TextBoxFor(model => model.Id)</td>

for all the fields so far (there are several hundred in each view.

It doesnt make sense for me to have to go into these and change many of them to HiddenFor as I have to do it in many places.

I want to know if there is an option to put it over the property of the view model like:

    [Display(Name = "ID:")]
    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }

in order to hide both the label and the input textbox at once so only the fields I have marked show up. I am thinking that I shoudl be able to just set an attribute to do this so where am I going wrong.

Thanks in advance for any help.

Francis Rodgers
  • 4,565
  • 8
  • 46
  • 65
  • That's the best practice way to do it though, anything else and it's gonna get ugly :) – Mathew Thompson May 21 '12 at 11:53
  • you can define an EditorTemplate working with Attributes metadata. And emit accordingly (may be not best practice way, but it's clean and neat) Take a look at this: http://stackoverflow.com/questions/3828985/get-value-from-custom-attribute-in-editor-template – BigMike May 21 '12 at 11:57
  • Seems like the closest thing I can get to what I want is to comment out the TR's on the view. Thanks for your answers though.# – Francis Rodgers May 21 '12 at 13:38

1 Answers1

0

No, in your case, there is no out of box solution for that.

There's ScaffoldColumnAttribute that controls generation of input and label together, but it can be seen in action only when you use dynamically generated views, using EditorForModel. And this technique completely removes html, which is not the same as generating hidden inputs.

You could try to manage that using jQuery and attribute ends with selector though

<script>
$(function () {
    $('input[name$="Id"]').closest('tr').hide();
});
</script>
archil
  • 39,013
  • 7
  • 65
  • 82
  • Problem with this is you got to generate one line for each field you want to hide. At worst, I was willing to do one attribute close to where the field was defined in the viewmodel. But thanks for the answer anyway. – Francis Rodgers May 21 '12 at 13:37
  • Actually, you will need one selector for each field, and only in cases where field name does not fall in some common logic. For example, for all the properties, whose names end with Id, example code will work without change – archil May 21 '12 at 13:47