Jamie Curtis
Jamie Curtis

Reputation: 916

Keep partial form data when switching localization?

I used this great guide to localize my ASP.NET MVC 2 application, which I followed almost to the letter. The app is mainly form based.

I was wondering if there was an easy way to be able to switch between languages in the middle of filling out a form without clearing the whole form, and having to start over? If not, could you suggest a way of localizing an application that would support this?

Maybe that's not a thing...

Upvotes: 1

Views: 202

Answers (1)

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24088

The approach used in the article is not the best one to keep the localisation I guess.

But what you can do is the following:

  1. Handle event when user clicks on the language link.
  2. Change language via ajax preventing browser to go to the actual link.
  3. Submit the form that user is editing adding parameter saying "make sure you don't save".
  4. The server would re-render the form as normally with the data posted, but in the new language.

JavaScript pseudocode:

var submitCurrentForm = function() {
  $("form:last").submit({
    data { dontSave: "True"} // this is just meta, you can use QueryString or hidden input
  });
}

var switchLanguage = function(href, done) {
  $.post(href).success(done); // using jQuery deferred
}



$("a.lang").click(function(e) {
  e.preventDefault();
  switchLanguage(this.href, submitCurrentForm);
});

Controller pseudocode:

public ActionResult Create(YourStuff stuff, bool dontSave = false) {
  if (!dontSave)
    ProcessTheStuff();
  return View(stuff);
}

Not the best solution, but the easiest one you can go with ATM.

Upvotes: 1

Related Questions