Hi @Kmcnet ,
If you want to implement multiple languages in MVC, you can choose to create resource files to switch languages.
The other issue is the relationship input which will be a dropdown with values such as mother, father, grandmother, etc.
You can do this by creating a DropDownList for the enum.
https://learn.microsoft.com/en-us/previous-versions/aspnet/ms227427(v=vs.100)
You can refer to the following example:
Resource files
Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
var languageSession = "en";
if (context != null && context.Session != null)
{
languageSession = context.Session["lang"] != null ? context.Session["lang"].ToString() : "en";
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageSession);
Thread.CurrentThread.CurrentCulture = new CultureInfo(languageSession);
}
}
Models
using System.ComponentModel.DataAnnotations;
namespace ResourceInMvc.Models
{
public class Test
{
[Display(Name = "FirstName", ResourceType = typeof(Resource))]
public string FirstName { get; set; }
[Display(Name = "LastName", ResourceType = typeof(Resource))]
public string LastName { get; set; }
[Display(Name = "Relationship", ResourceType = typeof(Resource))]
public Relationshiplist Relationship { get; set; }
public enum Relationshiplist
{
[Display(Name = "mother", ResourceType = typeof(Resource))]
mother,
[Display(Name = "father", ResourceType = typeof(Resource))]
father,
[Display(Name = "grandmother", ResourceType = typeof(Resource))]
grandmother,
}
}
}
HomeController
public ActionResult Index()
{
return View();
}
public ActionResult ChangeLanguage(string lang)
{
Session["lang"] = lang;
return RedirectToAction("Index", "Home", new { language = lang });
}
Index.cshtml
@model ResourceInMvc.Models.Test
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<a href="@Url.Action("ChangeLanguage", "Home", new { lang = "en" })">English</a> |
<a href="@Url.Action("ChangeLanguage", "Home", new { lang = "es" })">Spanish</a> |
<br />
@using (Html.BeginForm("Save", "Home"))
{
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
</div>
</div>
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Relationship, htmlAttributes: new { @class = "col-form-label" })
<div class="col-form-txt">
@Html.EnumDropDownListFor(Model =>Model.Relationship)
</div>
</div>
<input type="submit" name="submit" value="Save" />
}
</body>
</html>
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.