Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hein 29 posts 158 karma points
    Feb 20, 2018 @ 08:45
    Hein
    0

    Translate labels to render inside the view

    I'm making a form in Umbraco 7.7.7 and I'll translate the labels. For this I use the Display-attribute and the UmbracoHelper class to get access to the dictionary.

    public class ContactViewModel 
    {
        private UmbracoHelper _helper = new UmbracoHelper();
    
        [Required]
        [Display(Name = _helper.Field("#First name"))] // ← error on this line.
        public string FirstName { get; set; }
    
        [Required]
        [Display(Name = _helper.Field("#Last name"))]  // ← error on this line.
        public string LastName { get; set; }
    
        [Required]
        [EmailAddress]
        [Display(Name = _helper.Field("#Email"))]      // ← error on this line.
        public string Email { get; set; }
    
        [Required]
        [Display(Name = _helper.Field("#Message"))]    // ← error on this line.
        public string Message { get; set; }
    
        // Some other properties...
    
        public ContactViewModel()
        {
            _helper = new UmbracoHelper(UmbracoContext.Current);
        }
    }
    

    Here is my code in my view:

    @using (Html.BeginUmbracoForm<FormController>("contact", FormMethod.Post))
    {
        @(Html.EditorFor<ContactViewModel>())
    
        <div class="form-control">
            <input type="submit" value="verzenden" class="submit-button" />
        </div>
    }
    

    But I've got this error on the marked lines:

    An object reference is required for the non-static field, method, or property ContactViewModel._helper.

    I've also tried with this code:

    private static UmbracoHelper _helper = new UmbracoHelper(UmbracoContext.Current);
    

    and convert the result of _helper.Field() to a string instead of IHtmlString.

    [Required]
    [Display(Name = _helper.Field("#First name").ToString())] // ← error on this line.
    public string FirstName { get; set; }
    

    But code now this error also on the marked lines:

    An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.

    From Stack Overflow I know this:

    Attributes are metadata and must be know at compile time. _helper.Field() is a method and its results cannot be determined until runtime. You need to use a constant, for example [Display(Name = "First name")].

    P.S.: I'm following this documentation from our.umbraco.org.

    Update: Is there a other way to do more or less the same thing?

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 20, 2018 @ 09:14
    Dan Diplo
    0

    I think you've already got your answer from SO. The issue is here:

    [Display(Name = _helper.Field("#First name").ToString())]
    

    The value of Name must be a compile-time constant. It cannot be determined at runtime. Examples of a compile-time constant would be:

    1. a string literal eg. "Foo"
    2. A string declared using the const keyword eg. const string foo = "Foo";

    What you can't use is a traditional variable - in other words, anything that can change value when Umbraco is running. At the moment you build your application the value of the Display(Name) attribute has to be known and immutable (fixed). From this point on it cannot change. So it cannot be a return value from a method, as you are using.

  • Hein 29 posts 158 karma points
    Feb 20, 2018 @ 09:19
    Hein
    0

    Is there an other option to do more or less the same thing?

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 20, 2018 @ 10:17
    Dan Diplo
    1

    I've seen a few articles on this, though don't have any personal experience. But the places I'd look are:

    https://creativewebspecialist.co.uk/2014/04/17/umbraco-mvc-regionalisation-of-error-messages/

    https://24days.in/umbraco-cms/2017/multilingual-validation-messages/

    However, you'd need to check these techniques work in latest Umbraco.

Please Sign in or register to post replies

Write your reply to:

Draft