代码之家  ›  专栏  ›  技术社区  ›  JoseMarmolejos

MVC2:使用ScaffoldColumn(false),但仍在DisplayForModel()上显示属性

  •  1
  • JoseMarmolejos  · 技术社区  · 14 年前

    public class PartyGorilla
    {
        [ScaffoldColumn(false)]
        public int Id{ get; set; }
    
        [DisplayName("Gorilla's Name")]
        [Required]
        public string Name{ get; set; }
    }
    

    在我的编辑/创建视图中使用EditorForModel()时,一切都按需要工作,但如果在详细信息视图中使用DisplayForModel(),则不会显示Id属性,这不是我想要的。我的问题是:我需要广告的另一个属性还是我需要定制一个?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Kevin Stricker    14 年前

    我只想加上 LabelFor DisplayFor 这个 id 列在 DisplayForModel

    您也可以创建一个单独的ViewModel,但对于它实际完成的任务来说,它有一个不干净的脚印:

    public class PartyGorillaView
    {
        public int Id{ get; set; }
    
        [DisplayName("Gorilla's Name")]
        [Required]
        public string Name{ get; set; }
    }
    
    public class PartyGorillaEdit
    {
        [ScaffoldColumn(false)]
        public int Id{ get; set; }
    
        [DisplayName("Gorilla's Name")]
        [Required]
        public string Name{ get; set; }
    }
    
        2
  •  4
  •   Steve Michelotti    14 年前

    尝试使用[HiddenInput]属性。您可以选择显示该值,但它不可编辑:

    [HiddenInput(DisplayValue = true)]
    public int Id { get; set; }
    

    或者根本不显示该值:

    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }