difference between: [ScaffoldColumn (false)] and [

2019-04-27 13:58发布

问题:

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()`.

回答1:

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.



回答2:

I also wanted to know the difference, the following is from MSDN - http://msdn.microsoft.com/en-us/library/dd411771(v=vs.95).aspx

"AutoGenerateField - A value that indicates whether the field is included in the automatic generation of user-interface elements such as columns. This value is used by the DataGrid control."

From this it appears that this particular property is meant for DataGrid only.