2016-07-06 82 views
0

我看過這個https://www.youtube.com/watch?v=oGeAYd3idBc,我能夠創建多語言應用程序,但我需要幫助。我試圖用法文和英文創建一個下拉框,而不是我現在看到的兩個列表項鍊接。有誰知道如何做到這一點? 這裏是我的控制器將鏈接更改爲C語言下拉列表

using System; 
using System.Collections.Generic; 
using System.Globalization; 
using System.Linq; 
using System.Threading; 
using System.Web; 
using System.Web.Mvc; 

namespace MultiLanguage.Controllers 
{ 
public class LanguageController : Controller 
{ 
// GET: Language 
public ActionResult Index() 
{ 
    return View(); 
} 

public ActionResult Change(String LanguageAbbrevation) 
{ 
    if(LanguageAbbrevation !=null) 
    { 
     Thread.CurrentThread.CurrentCulture =  CultureInfo.CreateSpecificCulture(LanguageAbbrevation); 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation); 
    } 
    HttpCookie cookie = new HttpCookie("Language"); 
    cookie.Value = LanguageAbbrevation; 
    Response.Cookies.Add(cookie); 

    return View("Index"); 
} 

} 這裏是我的看法

@{ 
ViewBag.Title = "Index"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<ul> 
<li>@Html.ActionLink("English","Change","Language",new {LanguageAbbrevation = "en"}, null)</li> 


<li>@Html.ActionLink("French", "Change", "Language", new {LanguageAbbrevation = "fr" }, null)</li> 
<li>@DateTime.Now.ToString()</li> 
</ul> 

回答

0

這裏是控制器和模型:

public class LanguageModel 
{ 
    public List<SelectListItem> LanguageList { get; set; } 
    public string LanguageAbbrevation { get; set; } 

    public static List<SelectListItem> GetList() 
    { 
     List<SelectListItem> list = new List<SelectListItem> 
     { 
      new SelectListItem { Text = "English", Value = "en" }, 
      new SelectListItem { Text = "French", Value = "fr" }, 
     }; 

     return list; 
    } 
} 

public class HomeController : Controller 
{ 
    public ActionResult AAIndex() 
    { 
     LanguageModel securityQuestionModel = new LanguageModel(); 
     securityQuestionModel.LanguageList = LanguageModel.GetList(); 

     return View("AAIndex", securityQuestionModel); 
    } 

    [HttpPost] 
    public ActionResult Change(String LanguageAbbrevation) 
    { 
     if (LanguageAbbrevation != null) 
     { 
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation); 
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation); 
     } 
     HttpCookie cookie = new HttpCookie("Language"); 
     cookie.Value = LanguageAbbrevation; 
     Response.Cookies.Add(cookie); 

     return View("AAIndex"); 
    } 

這裏是視圖:

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@model Testy2.Controllers.LanguageModel 

@using (Html.BeginForm("Change", "Home")) 
{ 
    <ul> 
     <li>@Html.ActionLink("English", "Change", "Home", new { LanguageAbbrevation = "en" }, null)</li> 
     <li>@Html.ActionLink("French", "Change", "Home", new { LanguageAbbrevation = "fr" }, null)</li> 
     <li>@DateTime.Now.ToString()</li> 
    </ul> 

    <div class="form-group"> 
     <label>Language Dropdown</label> 
     @Html.DropDownListFor(m => m.LanguageAbbrevation, new SelectList(Model.LanguageList, "Value", "Text", 
       "English"), 
       new { @class = "form-control", onchange = "this.form.submit();" }) 

    </div> 
}