Hi yogeshc,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Class
SiteLanguage
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
public class SiteLanguage
{
public static List<Language> languages = new List<Language>
{
new Language { LanguageName = "English", CultureName = "en" },
new Language { LanguageName = "French", CultureName = "fr" }
};
public void SetLanguage(string language)
{
bool isLanguageAvailable = languages.Where(a => a.CultureName.Equals(language)).FirstOrDefault() != null ? true : false;
if (!isLanguageAvailable)
{
language = languages[0].CultureName;
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture((new CultureInfo(language)).Name);
HttpCookie httpCookie = new HttpCookie("culture", language);
httpCookie.Expires = DateTime.Now.AddDays(1);
HttpContext.Current.Response.Cookies.Add(httpCookie);
}
}
public class Language
{
public string LanguageName { get; set; }
public string CultureName { get; set; }
}
BaseController
using System;
using System.Web;
using System.Web.Mvc;
public class BaseController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
HttpCookie httpCookie = Request.Cookies["culture"];
string language = httpCookie != null ? httpCookie.Value :
(Request.UserLanguages != null ? Request.UserLanguages[0] : SiteLanguage.languages[0].CultureName);
new SiteLanguage().SetLanguage(language);
return base.BeginExecuteCore(callback, state);
}
}
HomeController
public class HomeController : BaseController
{
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(entities.Customers.Take(5).ToList());
}
public ActionResult ChangeLanguage(string language)
{
new SiteLanguage().SetLanguage(language);
return RedirectToAction("Index", "Home");
}
}
View
_ViewStart
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
_Layout
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
Language:
<select id="ddlLanguages">
@foreach (var language in Multilingual_MVC.SiteLanguage.languages)
{
<option value="@language.CultureName">@language.LanguageName</option>
}
</select>
<hr />
@RenderBody()
</body>
</html>
Index
@model List<Multilingual_MVC.Customer>
@{
ViewBag.Title = Resources.Resource.Index;
}
@using (Html.BeginForm())
{
<h4>@Resources.Resource.Greetings</h4>
<p>@Resources.Resource.Introduction</p>
<hr />
<table>
<thead>
<tr>
<th>@Resources.Resource.CustomerId</th>
<th>@Resources.Resource.Name</th>
<th>@Resources.Resource.Country</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.Country</td>
</tr>
}
</tbody>
</table>
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
if (document.cookie != '') {
var culture = document.cookie.split("=")[1];
$("#ddlLanguages").val(culture);
}
$("#ddlLanguages").change(function () {
var language = $(this).val();
location.href = '/Home/ChangeLanguage?language=' + language;
});
})
</script>
Screenshot