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

笑着哭i 提交于 2020-01-02 03:35:27

问题


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.



来源:https://stackoverflow.com/questions/7521204/difference-between-scaffoldcolumn-false-and-display-autogeneratefield-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!