0

I use Microsoft's code to localize my first web app.

I see here that the list is not empty

@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
        .ToList();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}

But the dropdown list is empty

<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="Home" 
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl" 
          method="post" class="form-horizontal" role="form">
        <label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label> <select name="culture"
          onchange="this.form.submit();"
          asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems">
        </select>
    </form>
</div>

What is wrong in this code?

The generated HTML looks like this

    <div title="Request culture provider: AcceptLanguageHeaderRequestCultureProvider">
        <form id="selectLanguage" asp-controller="Home" asp-action="SetLanguage" asp-route-returnUrl="/" method="post" class="form-horizontal" role="form">
            Language: <select name="culture" asp-for="de" asp-items="cultureItems"></select>
        </form>
    </div>

Here is another example which also has the same issues. https://github.com/aspnet/Entropy/blob/master/samples/Localization.StarterWeb/Views/Shared/_SelectLanguagePartial.cshtml

cerahe3782
  • 39
  • 6
  • I create a demo without any problem, do you mean `cultureItems` has correct data while the dropdown does not show them (both of the code are in the partial view)?Do you use 3.0 and could you show your complete code (partial view, startup.cs...) – Ryan Feb 18 '20 at 03:17
  • @XingZou right, but I don't use the partial view, I use directly the main view. Maybe this is the problem? – cerahe3782 Feb 18 '20 at 07:39
  • Do you put the two code blocks on the same razor view?it should also work. I suggest that you could create a simple demo with a new project.Could you show the screenshot of your debugger and startup code? – Ryan Feb 18 '20 at 08:03
  • @XingZou yes, everything in one single razor view. The output of the browser is fine as well as the output of the visual studio. I have updated the question and put the generated dropdown list there – cerahe3782 Feb 18 '20 at 12:36
  • @XingZou however, this works `@Html.DropDownList("selectLanguage", cultureItems)`, but I need to call onchange for it. Could you please help me with that? – cerahe3782 Feb 18 '20 at 12:44
  • I do not understand why – Ryan Feb 19 '20 at 02:10

1 Answers1

1

If you would like to use @Html.DropDownList,try to use below code:

@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName ,Selected = (c.Name == requestCulture.RequestCulture.UICulture.Name )})
        .ToList();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="Home"
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
          method="post" class="form-horizontal" role="form">
        <label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>

        @Html.DropDownList("culture", cultureItems, new
           {
               onchange = @"this.form.submit();"
           })

    </form>
</div> 

Action:

[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
Igor F.
  • 2,649
  • 2
  • 31
  • 39
Ryan
  • 19,118
  • 10
  • 37
  • 53