I have been tasked by my employer to build out a very simple customer satisfaction survey. The only catch is that it must be available in both English and Spanish. And, no database can be involved in handling the language. In other words, the actual words must be stored in the code, as variables. To make things simple (for the purpose of this discussion) let's just say that we need to display a single sentence, either in English or Spanish, depending on what parameter is on the end of the URL.

Given these two sentences:

"Display this sentence on the screen."

and

"Mostrar la frase en la pantalla."

I need some help finishing out the Model, the View, and the Controller. Where, how, would you store these two sentences in variables? Keep in mind there will eventually be a LOT of sentences, so the solution needs to scale.

Routing:

routes.MapRoute(
    "PatientSatisfactionSurvey",
    "PatientSatisfactionSurvey/{*LanguageCode}",
    new { controller = "Forms", action = "PatientSatisfactionSurvey" },
            namespaces: new[] { "xxx.Controllers" }
        );

Model:

namespace GEDC.Models
{
    public class PatientSatisfactionSurveyPage : Page
    {
        public List<string> LanguageOptions { get; set; }
        public string LanguageCode { get; set; }
        public PatientSatisfactionSurveyPage() { }
    }
}

Controller:

[HttpGet]
public ActionResult PatientSatisfactionSurvey(string LangCode)
    {
        PatientSatisfactionSurveyPage pss = new PatientSatisfactionSurveyPage();

        // Create list of available language options
        pss.LanguageOptions = new List<string> { "English", "Espanol" };

        // Check the incoming Language Code (LanCode) against the list off approved options
        if (!string.IsNullOrEmpty(LangCode))
        {
            if (pss.LanguageOptions.Contains(LangCode))
            {
                pss.LanguageCode = LangCode;
            }
            else
            {
                // if the LanCode is not found, default to English
                pss.LanguageCode = pss.LanguageOptions.First();
            }
        }
        else
        {
            // if the LanCode is not provided, default to English
            pss.LanguageCode = pss.LanguageOptions.First();
        }


    }

View:

I want to keep the logic to an absolute minimum here. As in, none. So, somewhere (in the controller, I assume?) Model.Sentence, or some such variable, will be populated with the correct version, depending on the parameter in the URL.

@Model.Sentence

Thanks in advance!!

Related posts

Recent Viewed