Search code examples
asp.netasp.net-mvcasp.net-mvc-4data-annotationsrazor-declarative-helpers

ASP.MVC concatenation in Html.LabelFor helper or model Display Name annotation


In my view i use the Html helper:

@Html.LabelFor(model => model.name)

The model contains the annotation for getting display name from resources

[Display(Name = "name", ResourceType = typeof(Resources.Models.ModelName))]

My resources provide translations without ":". If I put ":" after Html.LabelFor helper it won't be placed in label tag, but out of it:

<label for="name">Name</label>:

Is there any way to concatenate a postfix to the Html.LabelFor helper or the Display Name annotation?

Yes I know I can solve this problem by not using labelFor helper at all or concatenating ":" with jQuery, but I'm trying to find an elegant solution.


Solution

  • You can use css to add the : if you give your labels a class name:

    @Html.LabelFor(model => model.name, { @class = 'postfix'} )
    

    CSS:

    label.postfix:after
    {
         content: ":";
    }
    

    Or you can create an extension helper and call it as

    @Html.LabelForPostfix(model => model.name)