6

要在我的編輯視圖中呈現HTML,我使用助手@Html.EditorForModel()區別:[ScaffoldColumn(false)]和[Display(AutoGenerateField = false)]

我的模型:

[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; } 

物業:CodeGuest不應該由助手@Html.EditorForModel()創建。 (我想手動創建它。)

在互聯網上閱讀,我發現了幾點,並想知道其中的差異。

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

問:

記住的是,我不希望它被隱藏,這個領域將只通過該

EditorTemplates(IsGuest.cshtml)創建

什麼是區別:[ScaffoldColumn (false)][Display (AutoGenerateField = false)]

爲什麼不能我製作了[Display (AutoGenerateField = false)] have the effect: 'do not generate the HTML field when calling @ Html.EditorForModel()`。

回答

2

EditorForModel()DisplayForModel() Html幫助程序方法根據ViewData.ModelMetadata決定渲染當前模型屬性的視圖。默認DataAnnotationsModelMetadataProvider根據DataAnnotation屬性設置ModelMetadata的屬性。

ScaffoldColumnAttribute.Scaffold影響ModelMetadata的兩個性質,即'ShowForDisplay'和'ShowForEdit'。

DisplayAttribute不影響ModelMetadata的上述兩個屬性。

這就是爲什麼這兩個屬性在生成Html時沒有相同的效果。

相關問題