difference between: [ScaffoldColumn (false)] and [Display (AutoGenerateField = false)]

ridermansb picture ridermansb · Sep 22, 2011 · Viewed 7.4k times · Source

To render HTML in my edit view, I use the helper @Html.EditorForModel().

My model:

[Required(ErrorMessage = "Campo obrigatório")]
[Display(Name = "Nome completo")]
public string Name { get; set; }

[Required(ErrorMessage = "Campo é obrigatório")]
[StringLength(100, ErrorMessage = "A {0} deve ter pelo menos {2} characteres.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Senha")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirmar senha")]
[Compare("Password", ErrorMessage = "A nova senha e a confirmação da senha não conincidem.")]
public string ConfirmPassword { get; set; }

[Required(ErrorMessage = "Campo obrigatório")]
[Display(Name = "Convidado")]
[UIHint("IsGuest")]
public bool IsGuest { get; set; }

[RequiredIf("IsGuest", true, ErrorMessage = "Campo é obrigatório")]
[ScaffoldColumn(false)]
public string CodeGuest { get; set; }

Property: CodeGuest should not be created by the helper @Html.EditorForModel(). (I would like to create it manually.)

Reading on the Internet, I found several points and would like to know the difference.

Remembering that I do not want it to be hidden, this field will only be created by this

EditorTemplates (IsGuest.cshtml):

@using BindSolution.AndMarried.Model;
@model BindSolution.AndMarried.Models.RegisterModel
@Html.EditorFor(e => e.IsGuest)
<span>TESTE</span>
@Html.EditorFor(e => e.CodeGuest)

Question:

What is the difference between: [ScaffoldColumn (false)] and [Display (AutoGenerateField = false)]

Why can not I make [Display (AutoGenerateField = false)] have the effect: 'do not generate the HTML field when calling@Html.EditorForModel()`.

Answer

Kibria picture Kibria · Mar 3, 2013

The EditorForModel() and DisplayForModel() Html helper methods makes decision of rendering view of the properties of the current Model based on the ViewData.ModelMetadata. The default DataAnnotationsModelMetadataProvider sets the properties of ModelMetadata based on the DataAnnotation attributes.

ScaffoldColumnAttribute.Scaffold affects two properties of ModelMetadata i.e. 'ShowForDisplay' and 'ShowForEdit'.

DisplayAttribute does not affects the above two properties of ModelMetadata.

That is why these two attributes do not have the same effect on generating Html.