Multi-Language MVC Form

Kmcnet 1,256 Reputation points
2023-02-12T01:49:06.84+00:00

Hello everyone and thanks for the help in advance. I want to create a MVC form that can be used in English and Spanish. It is a very simple model of FirstName, LastName, and Relationship. I understand how to use Data Annotations to create the labels, but don't know how to change them depending on the language. The other issue is the relationship input which will be a dropdown with values such as mother, father, grandmother, etc. I assume I can populate from a parameter passed to the controller, but don't know how to link this to the annotations. Any help would be appreciated.

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Lan Huang-MSFT 30,206 Reputation points Microsoft External Staff
    2023-02-13T06:01:29.8833333+00:00

    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

    User's image

    User's image

    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>
    

    1

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.