简体   繁体   中英

@Html.ActionLink For Language

How do I convert this to HTML action link

if (!string.IsNullOrEmpty(requestCulture.ToString()))
 {
     foreach (var item in cultureItems)
     {
         if (item.Value.Equals(requestCulture.ToString(), StringComparison.InvariantCultureIgnoreCase))
         {
             item.Selected = true;
             break;
         }
     }
 }

I'm able to generate the link by using

if (!string.IsNullOrEmpty(requestCulture.ToString()))

{ foreach (var item in cultureItems) { if (item.Value.Equals(requestCulture.ToString(), StringComparison.InvariantCultureIgnoreCase)) { <a href=“@url”>@item.Name</a> } } }

But how do I generate the link for href that changes the language on the page. href=/fr-ca

<a>FR</a> <a>EN</a>

Because the parameter of ActionLink is wrong. It cannot generate correct action link. @Html.ActionLink can config five parameters. The first and second parameter are required.

The first parameter is the text that will show in the view. The second parameter is action. The third parameter is routevalues. Here is the example (Index.cshtml).

@Html.ActionLink("text","get", new { key="item", value = "fr - ca" })

It is parsed by browser in this format.

<a href="/Home/get?key=item&amp;value=fr%20-%20ca">text</a>

In HomeController.

    public IActionResult get(string key,string value)
    {
        return Json($"{key} {value}");
    }

Result. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM