0

the SiteLanguage cs file:

 public class SiteLanguages
        {
            public static List<Languages> AvailableLanguages = new List<Languages>
            {
                 new Languages{ LangFullName = "English", LangCultureName = "en"},
                 new Languages{ LangFullName = "Español", LangCultureName = "es"},
                 new Languages{ LangFullName = "বাংলা", LangCultureName = "bn"}
            };
     public class Languages
        {
            public string LangFullName { get; set; }
            public string LangCultureName { get; set; }
        }
    }

cshtml file:

@{
            foreach (var i in MvcMultilingual.SiteLanguages.AvailableLanguages)
            {
                @Html.ActionLink(i.LangFullName, "ChangeLanguage", "Home", new{lang = i.LangCultureName}, null) <text>&nbsp;</text>
            }
        }

I want to convert this action list group to dropdown list. How to change this code? I mean I just want to change cshtml side. Html.ActionLink to Html.DropdownList etc.

Hgrbz
  • 185
  • 3
  • 12
  • 1
    Does this answer your question? [is it posible do have Html.ActionLink inside a DropDownList Without java script?](https://stackoverflow.com/questions/6088042/is-it-posible-do-have-html-actionlink-inside-a-dropdownlist-without-java-script) – Yong Shun Nov 14 '21 at 11:26
  • No I have seen this. I'm asking how can i convert action group to dropdown list. – Hgrbz Nov 14 '21 at 13:34
  • you can't convert action link to drop down. but you can convert AvailableLanguages list to SelectListItem which can easily go with razor dropdown. you can check this - https://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc – Asif Rahman Nov 14 '21 at 15:40
  • I am using AvailableLanguages list other places. I don't want to change it to SelectListItem. Can't i change just ActionLink to DropdownList? – Hgrbz Nov 14 '21 at 16:06

1 Answers1

0

Try the following:

@using Languages = MvcMultilingual.SiteLanguages

@Html.DropDownListFor(m => Languages.AvailableLanguages.GetEnumerator().Current,
    Languages.AvailableLanguages.Select(d =>
    {
        return new SelectListItem() {
            Text = d.LangFullName,
            Value = Url.Action("SetLanguage", "Home", new { lang = d.LangCultureName })
        };
    }),
    "-Select Language-",
    new { id = "urlddl" })

See the javascript function for processing the change event in this post: is it possible do have Html.ActionLink inside a DropDownList Without java script?

Processing the selected value on the controller side:

public ActionResult SetLanguage(string lang)
{
    ...
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33