0

I want to create a multilingual webpage. To switch between languages I've got a dropdown on my page. If the change event of the dropdown gets fired the Method called "ChangeLanguage" in my Controller is called.

public ViewModels.HomeViewModel HVM { get; private set; }
    // GET: Home
    public ActionResult Index()
    {
        this.HVM = new ViewModels.HomeViewModel();
        return View(this.HVM);
    }

    public JsonResult ChangeLanguage(int id) {
        return Json(new {Success = true});
    }

Now I'd like to to change my "SelectedLanguage" Property in my ViewModel (HVM) - but the Reference is null. May anyone explain why HVM is null in my ChangeLanguage Method?

After my SelectedLanguage Property is changed I'd like to reload my whole page to display it's texts in another language

e.g.

@model ViewModels.HomeViewModel
<html>
<div class="HeaderText">
    Text = @{
        @Model.TextToDisplay.Where(o => 
        o.Language.Equals(Model.SelectedLanguage)).First()
    }
</div>

Here's what I want to do in PseudoCode:

PseudoCode:

public JsonResult ChangeLanguage(int id) {
    this.HVM.SelectedLanguage = 
    this.HVM.AvailableLanguages.Where(o => 
    o.ID.Equals(id)).First();

    Page.Reload();
    return Json(new {Success = true});
}
2
  • Because you have not initialized a new instance of HVM (when you call a controller method, a new instance of the controller is created - you don't have access to a property you set in a previous call to a get method). You need to persist the value somewhere (e.g. a database) Commented Mar 31, 2015 at 5:55
  • And your making and ajax call so not much point in "reload my whole page". Either return json with the new values you want to update in the DOM (see my answer to your last question) or change the method to return a partial view based on the new language and update the DOM Commented Mar 31, 2015 at 6:07

2 Answers 2

1
May anyone explain why HVM is null in my ChangeLanguage Method?

Adhering to stateless nature of HTTP protocol, all (unless explicitly added into request header) requests (MVC method calls) loose state data associated with it. Web server treats every request a new request and creates new instances of classes right from controller itself.

In your case since it is a new request, controller has a HVM property defined but in ChangeLanguage it is not instantiated (it gets instantiated only into Index method which is not called when you invoke ChangeLanguage) hence it is null.

After my SelectedLanguage Property is changed I'd like to reload my whole page to display it's texts in another language.

Option 1: Refresh page

Simple option to implement. Pass the language selection to server, server will return a new view with specific data. Drawback, whole page will refresh.

Option 2: Update view selectively

If option 1 is really not acceptable, then consider this option. There are multiple ways you can achieve it. Basically it involves either (a) breaking you view into partial view and update only the portion that is affect by selection or (b) bind data element with a JS object.

(a) - Not much need to be said for this.

(b) - Data binding can easily be done if you employ a JS library like KnockoutJS.

Sign up to request clarification or add additional context in comments.

3 Comments

@Chill-X - Added into my answer.
Thanks, refreshing the whole page should be fine - now I'm facing the cache-problem - I'm calling my ChangeLanguage Method which returns a new View - but my labels won't get changed (although the values that should be displayed differ). Is there a way to force the client not to use the cache while returning an ActionResult? I read something about random numbers in the URL but I really don't know how to do that using the ActionResult.
@Chill-X - You should not face this problem. I suspect it is to do with how you are calling and rendering the page. I suggest you add a new question with your view and controller code.
0

Change your methods to these methods , This trick will work for you =>pass your model to Change language from view. Also update JsonResult to ActionResult.

public ActionResult ChangeLanguage(ViewModels.HomeViewModel model,int id)
{
this.HVM.SelectedLanguage = 
this.HVM.AvailableLanguages.Where(o => 
o.ID.Equals(id)).First();
return RedirectToAction("Index",model);
}

public ActionResult Index(ViewModels.HomeViewModel model)
 {
if(model == null)
{
    this.HVM = new ViewModels.HomeViewModel();

}
return View(this.HVM);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.