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
ScaffoldColumn – Allows hiding fields from editor forms.
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.
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.
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.
just found the answer actually..
[HiddenInput(DisplayValue = false)]
works but I had to add:
using System.Web.Mvc;
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With