Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data annotations hide property/field

I have a model

class Address {
 public int AddressID {get;set;}
 public string Street {get;set;}
 public string City {get;set;}
 public string State {get;set;}
 public int ZipCode {get;set;}
}

in my view, when I have

@Html.LabelFor(model => model.Address) (assuming Address is a complex property inside another model)

I get a label for every one of Address properties, so I get:

AddressID:

Street:

City:

State:

ZipCode:

problem is, I don't want the ID property to show up, I tried these two annotations:

[Display(AutoGenerateField = false)]
[HiddenInput(DisplayValue = false)]

but the first one doesn't do anything for some reason, and the HiddenInput keeps getting a red squiggly line, not sure if they both use the same System.ComponentModel.DataAnnotations assembly

like image 389
duxfox-- Avatar asked Oct 02 '14 13:10

duxfox--


People also ask

Which data annotation attribute allows the hiding of fields from editor forms?

ScaffoldColumn – Allows hiding fields from editor forms.

Can we do validation in MVC using data annotations?

In ASP.NET MVC, Data Annotation is used for data validation for developing web-based applications. We can quickly apply validation with the help of data annotation attribute classes over model classes.

How do I use system ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

What is data annotations in MVC?

DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.


2 Answers

just found the answer actually..

[HiddenInput(DisplayValue = false)] works but I had to add:

using System.Web.Mvc;
like image 58
duxfox-- Avatar answered Sep 21 '22 14:09

duxfox--


as for me, when I'm used

[HiddenInput(DisplayValue = false)]

to my model props I still got displaying in schaffolded Create/Edit views. When I was just removing that code from view - yes, it wasn't visible any more, but after saving edits I got another problem: props for what I remove editors change their values to null. I use this on edit views to fix it

@Html.HiddenFor(model => model.ImageUrl)
like image 30
Dima Lohvynenko Avatar answered Sep 20 '22 14:09

Dima Lohvynenko