Question

I have an object like this :

public class AppointmentStatus 
{
     public int Id {get;set;}
     public string I18NKey {get;set;}
}

The I18NKey refers to a key for the translation.

In my form, I create a dropdownlist using a Select list :

Html.DropDownListFor(x=>x.Id, new SelectList(MyListOfStatus, "Id","I18NKey")

With this I retrieve the value of the key as text, I want to edit the property text in every SelectListItem.

I used something like this :

public static SelectList TranslateValue(SelectList list)
    {
        foreach (var tmp in list)
        {
            tmp.Text = I18nHelper.Message(tmp.Text);
        }
        return list;
    }

But it changes nothing ! The Text property is still the same, why ?

Was it helpful?

Solution 2

I finally found the solution : A SelectList seems to be based on the IEnumerable it needs. The property Text of every SelectListItem are dynamics and depends on every item of the IEnumerable.

We need to change the IEnumerable, not the SelectList :

What I finally did :

public class AppointmentStatus 
{
     public int Id {get;set;}
     public string I18NKey {get;set;}
     public string Translation {get;set;}
}

public static IEnumerable<AppointmentStatus> TranslateValue(IEnumerable<AppointmentStatus> list)
    {
        foreach (var tmp in list)
        {
            tmp.Translation = I18nHelper.Message(tmp.I18NKey);
        }
        return list;
    }

Html.DropDownListFor(x=>x.Id, new SelectList(HelperClass.TranslateValue(MyListOfStatus), "Id","Translation")

Hope that it will help you.

OTHER TIPS

You need to change the binding in drop down:

Html.DropDownListFor(x => x.Id, 
       TranslateValue(new SelectList(MyListOfStatus, "Id","I18NKey"))

Also check if your I18nHelper.Message works as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top