Html.LabelFor Specified Text

后端 未结 7 1567
渐次进展
渐次进展 2020-12-24 04:28

Anyone got any idea on how to specify text when using Html.LabelFor(c=>c.MyField). It\'s just MyField might not be an appropriate name to displ

相关标签:
7条回答
  • 2020-12-24 05:03

    I haven't downloaded v2 yet, so I can't test, but I believe it works like DynamicData, in which case you'd do something like this on your model:

    [Display(Name = "The Super Fantastic Field")]
    public string MyField {get;set;}
    
    0 讨论(0)
  • 2020-12-24 05:03

    There are two ways
    1"direct annotations"
    2"Annotatinos with a resource"
    Direct annotations

    [Display(Name = "My Field")]
    public string MyField { get; set; }
    

    Annotatinos with a resource

    [Display(Name = "My_Field",ResourceType = typeof(Resource))]
    public string MyField { get; set; }
    

    Second way will require to add a value in resource file probably named as Resource.resx.
    Use which suits your purpose.

    0 讨论(0)
  • 2020-12-24 05:07

    I haven't checked out CP1 yet but I read over Scott's release of it and I seem to recall that the code was generated by T4. I suppose you could always mod that, but I would suspect that they will provide overloads in CP2.

    Edit: The source is always available and thus you could just mod the method, change the T4 generator, and you'll be good to go. Also put in a ticket or request (somehow) for that mod so it gets worked into the next version.

    0 讨论(0)
  • 2020-12-24 05:08

    You use System.ComponentModel.DataAnnotations.DisplayAttribute:

    [Display(Name = "My Field")]
    public string MyField { get; set; }
    

    Setting the ResourceType property on your attribute will allow you to use a resource file.

    (Prior to .NET 4 use System.ComponentModel.DisplayNameAttribute with the caveat that the display name must be a compile-time constant.)

    0 讨论(0)
  • 2020-12-24 05:10

    Easy solution just add the following in the view:

    @Html.LabelFor(c=>c.MyField, "My Field")
    
    0 讨论(0)
  • 2020-12-24 05:10

    There are 5 overloads. Several offer second parameter of "string labelText", which you would set to "The Super Fantastic Field".

    0 讨论(0)
提交回复
热议问题